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

Re: printing a range of lines only if subsequent records contain
entropic wrote:
> I'd like to print a range of lines from multiple files.  The printout
> should have the filename and, then, print out a range of records from
> line n-2 to n+1 of every file only if:
>
> 1. the record n contains pattern 'foo', and
> 2. record n+1 contains pattern 'bar', then
> 3. exit and read subsequent file.
>
> I know I should read in subsequent records into an array and then do
> the comparison, but the exact syntax evades me...

You don't need an array, since your offsets are small and constant,
something like the following code should do it...

/bar/ { if (flag) { print h2 ; print h1 ; print h0 ; print $0 ; nextfile } }
{ flag = 0; h2 = h1 ; h1 = h0 ; h0 = $0 }
/foo/ { flag = 1 }

Be aware that 'nextfile' is a gawk feature, if you don't have gawk use
'exit' and either feed the files separately to your script or implement
another state flag.

Janis

Report this thread to moderator Post Follow-up to this message
Old Post
Janis Papanagnou
10-14-04 01:55 AM


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 05:56 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.