Script to get required output from textfile

Hi

[LEFT]Iam running below script on one text file.

usr/xpg4/bin/awk 'BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/local/ {HOST=$1}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
' /Scripts/sub_report_$FILE>/Scripts/sub_final_report_.txt

the output is coming as below

IP

HOST_NAME SUB 
10.238.48.1 [local]bgl-ras-bng-bge-01#cont 12853
10.238.48.33 [local]bgl-ras-bng-bge-02#cont 9527
10.238.48.193 [local]bgl-ras-bng-jnr-03#cont 13475
10.238.48.225 [local]bgl-ras-bng-jnr-04#cont 10753

but I need script to get below output

IP HOST_NAME SUB 
10.238.48.1 bgl-ras-bng-bge-01 12853
10.238.48.33 bgl-ras-bng-bge-02 9527
10.238.48.193 bgl-ras-bng-jnr-03 13475
10.238.48.225 bgl-ras-bng-jnr-04 10753

can anybody help

tnx in advance.

[/LEFT]

Could you please paste a few lines from your input file for a sample?

What about adding sub(/\[local\]|#cont/, "") to your awk program?

Change

/local/ {HOST=$1}

to

/local/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}

.

Note that this is without looking at your input and just by looking at your output. It's always better to post input sample as balajesuri pointed out.

@RudiC, that's a sub . So, it will do only 1 replacement if possible.

Hi

below are few lines from input file

IP IS 10.238.48.225
spawn telnet 10.238.48.225
Trying 10.238.48.225...
Connected to 10.238.48.225.
Escape character is '^]'.

bgl-ras-bng-jnr-04

[bsnl.in]bgl-ras-bng-jnr-04#show sub sum all 
--------------------------------------------------------------------------------
Total=10066

Type            Authenticating          Active          Disconnecting
PPP                          0               0                      0
PPPoE                        2           10064                      1
DOT1Q                        0               0                      0
CLIPs                        0               0                      0
ATM-B1483                    0               0                      0
ATM-R1483                    0               0                      0
Mobile-IP                    0               0                      0
[bsnl.in]bgl-ras-bng-jnr-04#exit
Connection to 10.238.48.225 closed by foreign host.
IP IS 10.238.49.1
spawn telnet 10.238.49.1
Trying 10.238.49.1...
Connected to 10.238.49.1.
Escape character is '^]'.

chn-ras-bng-hdw-01

[local]chn-ras-bng-hdw-01#cont bsnl.in
[bsnl.in]chn-ras-bng-hdw-01#show sub sum all 
--------------------------------------------------------------------------------
Total=7567

Type            Authenticating          Active          Disconnecting
PPP                          0              19                      0
PPPoE                        4            7544                      5
DOT1Q                        0               0                      0
CLIPs                        0               0                      0
ATM-B1483                    0               0                      0
ATM-R1483                    0               0                      0
Mobile-IP                    0               0                      0
[bsnl.in]chn-ras-bng-hdw-01#exit
Connection to 10.238.49.1 closed by foreign host.
IP IS 10.238.49.33
spawn telnet 10.238.49.33
Trying 10.238.49.33...
Connected to 10.238.49.33.
Escape character is '^]'.

the modifed code is giving syntax error

code

root@blr-svr-oclan-01 # awk 'BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/local/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
'sub_report_20130210_10:00:00

output

awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 3
awk: illegal statement near line 3
awk: illegal statement near line 4
awk: syntax error near line 5
awk: bailing out near line 5
root@blr-svr-oclan-01 # 

I didn't ask you to change usr/xpg4/bin/awk to awk .

Hi

the script is working now but out put is not exactly as per requirement.

root@blr-svr-oclan-01 # /usr/xpg4/bin/awk 'BEGIN {print "IP HOST_NAME SUB "}
root@blr-svr-oclan-01 > /IP IS/ {IP=$3}
root@blr-svr-oclan-01 > /local/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
root@blr-svr-oclan-01 > /PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
root@blr-svr-oclan-01 > ' sub_report_20130210_10:00:00

output is coming as below

IP HOST_NAME SUB 
10.238.48.1 [local]bgl-ras-bng-bge-01 11165
10.238.48.33 [local]bgl-ras-bng-bge-02 8029
10.238.48.65 [local]bgl-ras-bng-bge-03 10364
10.238.48.97 [local]bgl-ras-bng-bge-04 11800

Required output as like below

IP HOST_NAME SUB 
10.238.48.1 bgl-ras-bng-bge-01 12853
10.238.48.33 bgl-ras-bng-bge-02 9527
10.238.48.193 bgl-ras-bng-jnr-03 13475

What about Post #3? You might consider using /.local.|#cont/ as a search pattern if your awk flavour doesn't like the \[...\] construct.
And, as elixir_sinari pointed out (thank you!) , use gsub in lieu of sub.

Hi

I tried the below code but output is not as desired.

root@blr-svr-oclan-01 # /usr/xpg4/bin/awk 'BEGIN {print "IP HOST_NAME SUB "}
root@blr-svr-oclan-01 > /IP IS/ {IP=$3}
root@blr-svr-oclan-01 > /.local.|#cont/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
root@blr-svr-oclan-01 > /PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
root@blr-svr-oclan-01 > ' /Scripts/sub_report.txt 

output is coming as below

IP HOST_NAME SUB 
10.238.52.65 [local]pun-ras-bng-mhs-01 13623
10.238.54.1 [local]enk-ras-bng-cse-01 19610
10.238.56.225 [local]ngp-ras-bng-cto-01 19090
root@blr-svr-oclan-01 # 

This should be one line of code. Ok,you can put the awk code in a file and run it like this:

# cat > script.awk
BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/.local.|#cont/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
#
# awk -f script.awk /path/to/Scripts/sub_report.txt
IP HOST_NAME SUB
10.238.48.225  10064
10.238.49.1 chn-ras-bng-hdw-01 7544
#

Hi

code is modifed,output is not coming

root@blr-svr-oclan-01 # more script.awk 
BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/.local.|#cont/ {HOST=$1;sub(/\[[^\]]*\]/,"",HOST);sub(/#.*/,"",HOST)}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}


root@blr-svr-oclan-01 # /usr/xpg4/bin/awk -f script.awk /Scripts/sub_report.txt 
IP HOST_NAME SUB 
10.238.52.65 [local]pun-ras-bng-mhs-01 13623
10.238.54.1 [local]enk-ras-bng-cse-01 19610
10.238.56.225 [local]ngp-ras-bng-cto-01 19090

output is like below

if iam using only awk ,then output is like below

root@blr-svr-oclan-01 # awk -f script.awk /path/to/Scripts/sub_report.txt
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 3
awk: illegal statement near line 3
awk: illegal statement near line 4
root@blr-svr-oclan-01 # 

Make that line read

/local/ {gsub (/.local.|#cont/,"");(HOST=$1;} 

Hi

modified the code but output is not coming this time
code

root@blr-svr-oclan-01 # more script.awk 
BEGIN {print "IP HOST_NAME SUB "}
/IP IS/ {IP=$3}
/local/ {gsub (/.local.|#cont/,"");(HOST=$1;}
/PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
root@blr-svr-oclan-01 # /usr/xpg4/bin/awk -f script.awk /Scripts/sub_report.txt

output

/usr/xpg4/bin/awk: file "script.awk": line 3: syntax error  Context is:
>>>     P=$3}
>>>     /local/ {gsub (/.local.|#cont/,"");(HOST=$1;    <<<
root@blr-svr-oclan-01 #

Sorry, one parenthesis too many due to sloppy copying... make it

/local/ {gsub (/.local.|#cont/,"");HOST=$1} 

Hi

thanks,

now the code is working perfectly.

code

root@blr-svr-oclan-01 # /usr/xpg4/bin/awk 'BEGIN {print "IP HOST_NAME SUB "}
root@blr-svr-oclan-01 > /IP IS/ {IP=$3}
root@blr-svr-oclan-01 > /local/ {gsub (/.local.|#cont/,"");HOST=$1}
root@blr-svr-oclan-01 > /PPPoE/ {SUB=$3 ;print IP, HOST, SUB}
root@blr-svr-oclan-01 > ' /Scripts/sub_report.txt
IP HOST_NAME SUB 
10.238.52.65 pun-ras-bng-mhs-01 13905
10.238.54.1 enk-ras-bng-cse-01 19348
10.238.56.225 ngp-ras-bng-cto-01 19309

can you please explain the below line

/local/ {gsub (/.local.|#cont/,"");HOST=$1} in detail

man awk is your friend... However,

gsub (                  # globally substitute pattern (= substring) in string
/                       # pattern start
.local.                 # <any single char>local<any single char>
|                       # or
#cont                   # "#cont"
/,                      # pattern end
"")                     # with empty string
1 Like

Hi,

Thanks for guidance.