Awk syntax needed

ashsysad used Ask the Experts™
I have a command output like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface   U         0 0          0 eth0   U         0 0          0 usb0     U         0 0          0 usb0         UG        0 0          0 eth0

I need an awk statement that will output the 3rd line of the output and assigns to a varaible.   U         0 0          0 eth0

In addition, I want to assign "eth0" (last item) to another variable.  Please assist.

Thanks !
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


I'm getting syntax error:

# netstat -rn | awk {NR==3'{linethree=$0;linethreelast=$NF}'}
awk: {NR==3{linethree=$0;linethreelast=$NF}}
awk:       ^ syntax error

I'm not that comfortable with AWK. Please give me the full syntax.

Too many brackets!  The "NR==3" part is the bit which determines whether the command is run, and the command to run is in the brackets.

    netstat -rn | awk 'NR==3{linethree=$0;linethreelast=$NF}'

But you do then have to do something with the data.  For example:

    netstat -rn | awk 'NR==3{linethree=$0;linethreelast=$NF}
    END{printf "Device was %s, and last line was <%s>\n",linethreelast,linethree}'


Thankyou !

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial