1755639 Members
3244 Online
108837 Solutions
New Discussion юеВ

awk hack

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

awk hack

Hello all,

I have written a bit of a hack but am sure I can get the desired output within one statement:

# netstat -rn |awk '/default/ {IP = $2};END{print IP}'|awk -F"." '{print $1"."$2}'

172.20

what is the best syntax for running this with one awk?

thanks

Chris
hello
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: awk hack

Hi Chris:

# netstat -rn |awk '/default/ {split($2,a,".");print a[1]"."a[2]}'

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: awk hack

HI (again) Chris:

It's worth remembering that you can set your output record seperator to write the solution thusly, too:

# netstat -rn |awk 'BEGIN{OFS="."};/default/ {split($2,a,".");print a[1],a[2]}'

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: awk hack

ok nice one James - thanks again
hello