1826436 Members
4088 Online
109692 Solutions
New Discussion

Re: Scripting help

 
SOLVED
Go to solution
Samir Bittar
Occasional Contributor

Scripting help

Hello everyone
I'm having some trouble trying to write a script that filters some information from an output...
On a HP-UX B11.00 OS, when I type netstat -rn, I get a full listing with lots of stuff. I want to filter the line that has the word "default" written on it, and get the exact text that's under the INTERFACE column, so I can get that interface name...
initially I tried:
netstat -rn | grep default | awk '{print $5}'
it works fine in some places, but there are others where INTERFACE is NOT the 5th column, so how can I write a script to identify what column INTERFACE is so I can properly use the AWK command, or if you have any other solutions, please help! Thanks
5 REPLIES 5
John Poff
Honored Contributor
Solution

Re: Scripting help

Hi,

Which column is the Interface value in when it isn't in the fifth column?

If it is always in the next to last column you could use this awk snippet to get it:

netstat -rn | grep default | awk '{print $(NF-1)}'

JP
James R. Ferguson
Acclaimed Contributor

Re: Scripting help

Hi Samir:

John's approach is valid, but you don't need the extra process of 'grep':

# netstat -rn | awk '$1~/^default/ {print $(NF-1)}'

Regards!

...JRF...
Peter Godron
Honored Contributor

Re: Scripting help

Samir,
definitely not too nice, but seems to work:
#!/usr/bin/sh
#Find Interface offset
a=`netstat -rn | grep ^Dest`
#Find start column of Interface
start=`expr index "$a" "I"`
# Extract the data from the default line
a=`netstat -rn | grep default | cut -c ${start}-`
# Find first space
start=`expr index "$a" " "`
# Display until first space
echo $a | cut -c -${start}

Bharat Katkar
Honored Contributor

Re: Scripting help

Hi Samir,
See if this helps.

# netstat -rn | grep default | tr -s " " "\n" | grep lan


Regards,
You need to know a lot to actually know how little you know
Arturo Galbiati
Esteemed Contributor

Re: Scripting help

Hi,
with a one line command:
netstat -rn | awk '$1~/^default/ {print $(NF-1)}'
HTH,
Art