Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Selecting lines from stdin based on content of preceding line
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



Report this thread to moderator Post Follow-up to this message
Old Post
James Harris
05-24-05 08:56 PM


Re: Selecting lines from stdin based on content of preceding line
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


Report this thread to moderator Post Follow-up to this message
Old Post
hq00e@126.com
05-24-05 08:56 PM


Re: Selecting lines from stdin based on content of preceding line

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.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
05-24-05 08:56 PM


Re: Selecting lines from stdin based on content of preceding line
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


Report this thread to moderator Post Follow-up to this message
Old Post
hq00e
05-24-05 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:29 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.