Solved

Awk syntax needed

Posted on 2011-09-06
4
267 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 19

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 19

Accepted Solution

by:
simon3270 earned 200 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now