Solved

Awk syntax needed

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Utilizing an array to gracefully append to a list of EmailAddresses
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…

790 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