1753362 Members
5003 Online
108792 Solutions
New Discussion юеВ

Column "x" and row "y"

 
SOLVED
Go to solution
UniRock
Regular Advisor

Column "x" and row "y"


Hi All,

How can I get the data listed in column "x" and row "y" ??
Example, 2nd column and 4th row contains IP 192.168.15.112

# netstat -rn
Routing tables
Destination Gateway Flags Refs Interface Pmtu
127.0.0.1 127.0.0.1 UH 0 lo0 4136
192.168.15.112 192.168.15.112 UH 0 lan0 4136
16.16.158.112 16.16.158.112 UH 0 lan1 4136
192.168.15.0 192.168.15.112 U 2 lan0 1500
16.16.158.0 16.16.158.112 U 2 lan1 1500
127.0.0.0 127.0.0.1 U 0 lo0 0
default 16.16.158.1 UG 0 lan1 0

Thanks..
9 REPLIES 9
Stephan.
Honored Contributor
Solution

Re: Column "x" and row "y"

Hi,

netstat -rn | sed '4q;d' | awk '{print $2}'

4 = row, starting with 1

2 = column, starting with 1

hth
Suraj K Sankari
Honored Contributor

Re: Column "x" and row "y"

Hi,

You can do it like this way
netstat -rn | sed -n 4p | awk '{print $2 }'
or
netstat -rn | head -n 4 | awk '{print $2 }

This will extract 2nd column and 4th row.

Suraj



UniRock
Regular Advisor

Re: Column "x" and row "y"

Thanks a lot SVO and Suraj,

>> svo
netstat -rn | sed '4q;d' | awk '{print $2}' <== perfect

>> Suraj
netstat -rn | sed -n 4p | awk '{print $2 }' <== perfect

Can you please explain the meaning of:
sed '4q;d'
and
sed -n 4p

Regards
Suraj K Sankari
Honored Contributor

Re: Column "x" and row "y"

Hi,

sed -n 4p means line 4

for more information on sed see the link below it will help you lot

http://www.grymoire.com/Unix/Sed.html

Suraj
Stephan.
Honored Contributor

Re: Column "x" and row "y"

>> Can you please explain the meaning of:
sed '4q;d'

Quit after the 4th line and delete all before

sed -n 4p

Print line Nr 4

Quit & delete is the better choice in large files, result is the same.
Suraj K Sankari
Honored Contributor

Re: Column "x" and row "y"

Hi,

Is this not worked for you
netstat -rn | head -n 4 | awk '{print $2}'

Suraj
UniRock
Regular Advisor

Re: Column "x" and row "y"

NO...this gives the 2nd column till 4th row.

# netstat -rn | head -n 4 | awk '{print $2}'
tables
Gateway
127.0.0.1
10.20.180.151
Dennis Handly
Acclaimed Contributor

Re: Column "x" and row "y"

You can use awk to select the row and column:
netstat -rn | awk -v ROW=4 -v COLUMN=2 '
NR == ROW {
print $COLUMN
exit
}'
UniRock
Regular Advisor

Re: Column "x" and row "y"

Hi Again,

>> Dennis, I tried this and it worked beautifully..thanks a lot !!
# netstat -rn | awk -v ROW=4 -v COLUMN=2 'NR==ROW {print $COLUMN;exit}'