For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2005 > Selecting lines from stdin based on content of preceding line









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Selecting lines from stdin based on content of preceding line
James Harris

2005-05-24, 3:56 pm

I previously wrote a very simple but handy shell script wrapper for
nslookup which used awk and extracted the

Name:
Address{es}:

lines and worked very well but I now have to run this on a machine where
nslookup gives the server address as well as the target address such as:

C:\>nslookup 192.168.1.1
Server: ns0.zen.co.uk
Address: 212.23.8.1

Name: my.router
Address: 192.168.1.1

I am matching for "Address" but don't want the second line of the above.
Can awk be told to ignore this line if it reads "Server:" on the
preceding line?

TIA


hq00e@126.com

2005-05-24, 3:56 pm


James Harris
>
> C:\>nslookup 192.168.1.1
> Server: ns0.zen.co.uk
> Address: 212.23.8.1
>
> Name: my.router
> Address: 192.168.1.1
>
> I am matching for "Address" but don't want the second line of the

above.
> Can awk be told to ignore this line if it reads "Server:" on the
> preceding line?


Yes.
awk "/Name:/{a=$0}/Address:/ && a {print a\"\n\"$0;a=0}"
Awk is powerful but it is not the only choice:
In Sed:
gsed -n "/Name:/{N;p}"
Or in grep
grep -A1 Name:

_____
hq00e

Ed Morton

2005-05-24, 3:56 pm



James Harris wrote:
> I previously wrote a very simple but handy shell script wrapper for
> nslookup which used awk and extracted the
>
> Name:
> Address{es}:
>
> lines and worked very well but I now have to run this on a machine where
> nslookup gives the server address as well as the target address such as:
>
> C:\>nslookup 192.168.1.1
> Server: ns0.zen.co.uk
> Address: 212.23.8.1
>
> Name: my.router
> Address: 192.168.1.1
>
> I am matching for "Address" but don't want the second line of the above.
> Can awk be told to ignore this line if it reads "Server:" on the
> preceding line?


It sounds like all you need to do is:

awk '/Name/,/Address/'

Regards,

Ed.
hq00e

2005-05-24, 3:56 pm


Ed Morton =E5=86=99=E9=81=93=EF=BC=9A
> It sounds like all you need to do is:
>
> awk '/Name/,/Address/'
>
> Regards,
>
> Ed.


Hey, what...
how dare you...
..=2E..........
Very enlightening!
Everyone who peep the answer would/should
say I should have known the answer...but
they just can't find the answer before they
know it.
Commonly people learn sth new and forget
good-old-basic and basic one.

It is "=E7=A6=85" in Chinese.

I should have known that!
_____
hqQQe

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com