?
Solved

Awk syntax needed

Posted on 2011-09-06
4
Medium Priority
?
281 Views
Last Modified: 2012-06-21
I have a command output like this:

Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
10.20.66.0      0.0.0.0         255.255.255.0   U         0 0          0 eth0
169.254.95.0    0.0.0.0         255.255.255.0   U         0 0          0 usb0
169.254.0.0     0.0.0.0         255.255.0.0     U         0 0          0 usb0
0.0.0.0         10.20.66.1      0.0.0.0         UG        0 0          0 eth0


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

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

Thanks !
0
Comment
Question by:ashsysad
  • 2
  • 2
4 Comments
 
LVL 20

Expert Comment

by:simon3270
ID: 36489765
NR==3{linethree=$0;linethreelast=$NF}
0
 

Author Comment

by:ashsysad
ID: 36489939
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.

0
 
LVL 20

Accepted Solution

by:
simon3270 earned 800 total points
ID: 36491763
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}'
0
 

Author Closing Comment

by:ashsysad
ID: 36491856
Thankyou !
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Six Sigma Control Plans
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question