Code Comments
Programming Forum and web based access to our favorite programming groups.I want to find a pattern in a text file and delete the 2 lines in front of the found pattern to 1 line after the pattern throughout the file example: line 1 this is a test 2 to try to simulate 3 what I want to 4 do is this for real 5 text to look for 6 parrot 7 corn 8 apple 9 poll 10 this is the path I want to find the pattern "text to look for" and remove line 3 - 6 leaving line 1 this is a test 2 to try to simulate 3 corn 4 apple 5 poll 6 this is the path
Post Follow-up to this messageWow! How rare it is for someone to post the an example of their input
and output files! I wish everyone did this.
You will need to save/buffer your input and then print those lines a
little later, after you've determined that "text to look for" hasn't
appeared and therefore it is 'safe' to output those buffered lines.
But at the end of the program, you will have to print those lines
still in the buffer, you can't just exit your program.
I won't attempt to write the optimum or most efficient program, but
probably someone else will contribute one.
I did give this a quick test. It might not work correctly if "text to
look for" was the last line in the file or if you had two "text to
look for" lines consecutively.
{
# Save every line initially
line[NR] = $0
}
/text to look for/ {
# Delete two buffered lines and the current line
delete line[NR-2]
delete line[NR-1]
delete line[NR]
# Skip the next line
# It doesn't have to go into the buffer
getline
}
!/text to look for/ {
# Now it is safe to print one buffered line (if it exists)
if ((NR-2) in line) print line[NR-2]
delete line[NR-2]
}
END {
# Hmm. I don't know what the value of NR is here.
# I'll assume it's unchanged
# There are only two lines in the buffer
for (n = NR-1; n <= NR; n++) print line[n]
}
DKM
On 26 Aug 2004 07:56:39 -0700, tillman.h.craft@usa-spaceops.com
(usatim) wrote:
>I want to find a pattern in a text file and delete the 2 lines in
>front of the found pattern to 1 line after the pattern throughout the
>file example:
>
>line
>1 this is a test
>2 to try to simulate
>3 what I want to
>4 do is this for real
>5 text to look for
>6 parrot
>7 corn
>8 apple
>9 poll
>10 this is the path
>
>I want to find the pattern "text to look for" and remove line 3 - 6
>leaving
>line
>1 this is a test
>2 to try to simulate
>3 corn
>4 apple
>5 poll
>6 this is the path
To contact me directly, send EMAIL to (single letters all)
DEE_KAY_EMM AT EarthLink.net. [For example X_X_X@EarthLink.net.]
Post Follow-up to this message
usatim wrote:
> I want to find a pattern in a text file and delete the 2 lines in
> front of the found pattern to 1 line after the pattern throughout the
> file example:
>
> line
> 1 this is a test
> 2 to try to simulate
> 3 what I want to
> 4 do is this for real
> 5 text to look for
> 6 parrot
> 7 corn
> 8 apple
> 9 poll
> 10 this is the path
>
> I want to find the pattern "text to look for" and remove line 3 - 6
> leaving
> line
> 1 this is a test
> 2 to try to simulate
> 3 corn
> 4 apple
> 5 poll
> 6 this is the path
This should get you close (untested):
gawk '/text to look for/{ c=c-2; s=1; next }
s { s--; next }
{ a[++c]=$0 }
END {for(i=1;i<=c;i++) print a[i]}'
Regards,
Ed.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.