Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this message
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
Post Follow-up to this message
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.
Post Follow-up to this messageEd 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.