Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk - moving backwards

Status
Not open for further replies.

nashcom

MIS
Apr 12, 2002
91
GB
Hi

Is it possible to skip backwards a number of records when using awk? For example, if I'm reading each line in an input file and reach a line which begins with certain text I want to skip backwards a couple of lines in the input file and carry out some extra processing.

I may need to revisit the 'logic' in my script, but I thought I'd ask this question anyhow!

Thanks.
 
Thanks for confirming that. I'll tackle the script in a different way!
 
Here is an example of something like that. Every time it finds "four" it prints any records preceding that up to the previous "two" by accumulating them in an array.

Code:
awk '
        [green]/two/[/green] { i=0 }
        { a[++i]=[blue]$0[/blue] }
        [green]/four/[/green] { [olive]for[/olive] (j=1;j<=i;j++) [b]print[/b] a[j] ; i=0 }
' << EOF
one
two
three
four
five
one
two
three
two
three
two
three
four
three
four
five

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top