1847340 Members
2357 Online
110264 Solutions
New Discussion

Re: Help with awk

 
SOLVED
Go to solution
Don Spare
Regular Advisor

Help with awk

I am trying to write a script that will extract specific pieces of information from a file. The file (see attached) is the status of a Cisco switch port. It is 27 lines long but I only need pieces of it. I have put together an awk script to do this but its showing me only the output from the first condition for all lines rather than just the lines I'm looking for. The data I want is in lines 2, 19, 21, 23 & 24. Here is my awk.....

awk -v ptnum=$portnum '($2 == "is" && $3 == "up")
{print "Port# " ptnum " Status: " $3 " Protocol: " $7;next}
($2 == "packets" && $3 == "input")
{print " Pkt In: " $1 " Byte In: " $4;next}
($2 == "input" && $3 == "errors,")
{print " IN Errs: " $1;next}
($2 == "packets" && $3 == "output")
{print " Pkt In: " $1 " Byte In: " $4;next}
($2 == "output" && $3 == "errors,")
{print " OUT Errs: " $1 "Collisions: " $4;next}
' $pfile

Can someone point out what I am doing wrong?

10 REPLIES 10
Rodney Hills
Honored Contributor

Re: Help with awk

($2 == "packets" && $3 == "input")
($2 == "packets" && $3 == "output")

need to be changed to

($2 == "packets" && $3 == "input,")
($2 == "packets" && $3 == "output,")

You left off trailing comma.

HTH

-- Rod Hills

There be dragons...
Ramkumar Devanathan
Honored Contributor

Re: Help with awk

Hi,

I don't know what you've done - and am not going to debug your awk program - this is a neat way of doing this -

$ cat g.awk
/is up/ {print "Port# " ptnum " Status: " $3 " Protocol: " $7}
/packets input/ {print "Pkt In: " $1 " Byte In: " $4}
/packets output/ {print "Pkt Out: " $1 " Byte Out: " $4}
/output errors/ {print "Out Errors: " $1 " Collisions: " $4}

$ awk -v ptnum=$portnum -f g.awk $pfile

works with gnu awk for me.

HTH.

- ramd.
HPE Software Rocks!
James R. Ferguson
Acclaimed Contributor

Re: Help with awk

Hi:

I think part of your problem is the use of 'next'. This causes you to stop processing the current record and begin with the next one. Any subsequent rules for the *current* record, in that case, are never executed.

Regards!

...JRF...
Rodney Hills
Honored Contributor

Re: Help with awk

James,

I think the use of "next" is ok here, because "next" in awk means don't do any more pattern matches on the current input line. The script starts from the top with the next record.

-- Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: Help with awk

Hi Rod:

Yes, you're correct (of course). I'm not sure what I was thinking!

Warmest Regards!

...JRF...
Jean-Louis Phelix
Honored Contributor
Solution

Re: Help with awk

hi,

Again a new "feature" ... If you join line with patterns "(...)" and line with actions "{...}" it will work ... especially if you add trailings commas on all $3 patterns and change in to out on line 4.

Regards.

awk -v ptnum=$portnum '($2 == "is" && $3 == "up,") {print "Port# " ptnum " Status: " $3 " Protocol: " $7;next}
($2 == "packets" && $3 == "input,") {print " Pkt In: " $1 " Byte In: " $4;next}
($2 == "input" && $3 == "errors,") {print " IN Errs: " $1;next}
($2 == "packets" && $3 == "output,") {print " Pkt Out: " $1 " Byte Out: " $4;next}
($2 == "output" && $3 == "errors,") {print " OUT Errs: " $1 "Collisions: " $4;next}
' $pfile

/homeHA/phelix/tmp> sh a
Port# 0 Status: up, Protocol: up
Pkt In: 6771318 Byte In: 541384107
IN Errs: 56
Pkt Out: 7004964 Byte Out: 1791101868
OUT Errs: 0Collisions: 0
It works for me (© Bill McNAMARA ...)
Don Spare
Regular Advisor

Re: Help with awk

Jean Louis, your solution was exactly right. Thank you very much for your response.
Robin Wakefield
Honored Contributor

Re: Help with awk

Hi Don,

You need to escape the returns at the end of your match lines - this should work:

awk -v ptnum=$portnum '($2 == "is" && $3 == "up,"){print "Port# " ptnum " Status: " $3 " Protocol: " $7}
($2 == "packets" && $3 == "input,"){print " Pkt In: " $1 " Byte In: " $4}
($2 == "input" && $3 == "errors,"){print " IN Errs: " $1}
($2 == "packets" && $3 == "output,"){print " Pkt In: " $1 " Byte In: " $4}
($2 == "output" && $3 == "errors,"){print " OUT Errs: " $1 "Collisions: " $4}
' $pfile

rgds, Robin
Robin Wakefield
Honored Contributor

Re: Help with awk

Oh well, the browser's removed the backslash, and given you the previous solution!! I had a backslash character after each (...) line...

rgds, Robin
Don Spare
Regular Advisor

Re: Help with awk

Robin,
Once the single quote opens the commands section of the awk no continuation characters are required. Awk looks at it as being a script and not as a bunch of single commands. Of course, the script ends with a closing single quote.