[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Awk syntax needed

Posted on 2011-09-06
4
Medium Priority
?
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…

656 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