Newbie Question

T

tim

Help please..

I have to parse through a file for a pattern and if the pattern
matches, the preceding two and succeeding lines until a blank line is
encountered need to be redirected to another file. This has to be done
multiple times until EOF.

I have figured out the pattern matching and file redirection part. Can
anyone give hints regarding how to capture the preceedng two lines
after the pattern matches.

Thanks for your help.

tim
 
B

Bill Smith

tim said:
Help please..

I have to parse through a file for a pattern and if the pattern
matches, the preceding two and succeeding lines until a blank line is
encountered need to be redirected to another file. This has to be done
multiple times until EOF.

I have figured out the pattern matching and file redirection part. Can
anyone give hints regarding how to capture the preceedng two lines
after the pattern matches.

Thanks for your help.


How big is your input file? The problem is nearly trivial if it small
enough to read the entire file into an array. Otherwise, you may have
to trade off between complexity and execution time.

What should your program do if one of the first two or last two records
match? Can we ignore that possibility? Could it ever be necessary to
output the same record more than once (e.g. Two or more consecutive
lines match)?

What have you tried? Where did you have a problem?

Bill
 
S

Sherm Pendley

tim said:
I have to parse through a file for a pattern and if the pattern
matches, the preceding two and succeeding lines until a blank line is
encountered need to be redirected to another file. This has to be done
multiple times until EOF.

I have figured out the pattern matching and file redirection part. Can
anyone give hints regarding how to capture the preceedng two lines
after the pattern matches.

If you're using while(<>) to loop through the file a line at a time, you
could keep a rolling buffer that contains the previous two lines read.

At the end of your loop, push() the current line onto the end of the
buffer, and if the buffer has more than two lines in it, shift() the
first one off the beginning.

By the way, you should think about your subject lines - this wasn't a
question about newbies.

sherm--
 
J

John W. Krahn

tim said:
I have to parse through a file for a pattern and if the pattern
matches, the preceding two and succeeding lines until a blank line is
encountered need to be redirected to another file. This has to be done
multiple times until EOF.

I have figured out the pattern matching and file redirection part. Can
anyone give hints regarding how to capture the preceedng two lines
after the pattern matches.

perl -00ne'print /^ ( .*\n .*\n .* pattern (?s: .* ) ) \z/mgx' yourfile



John
 
J

John W. Krahn

tim said:
I have to parse through a file for a pattern and if the pattern
matches, the preceding two and succeeding lines until a blank line is
encountered need to be redirected to another file. This has to be done
multiple times until EOF.

I have figured out the pattern matching and file redirection part. Can
anyone give hints regarding how to capture the preceedng two lines
after the pattern matches.

perl -00ne'print /^ ( .*\n .*\n .* pattern (?s: .* ) ) \z/mx' yourfile


John
 
J

J. Romano

I have to parse through a file for a pattern and if the pattern
matches, the preceding two and succeeding lines until a blank line is
encountered need to be redirected to another file. This has to be done
multiple times until EOF.

I have figured out the pattern matching and file redirection part. Can
anyone give hints regarding how to capture the preceedng two lines
after the pattern matches.

Dear Tim,

I just create an array called @pastLines, that holds the past lines
encountered, whether they matched or not. I always trim it to just
two lines (so I don't have to store everything). When a match is
encountered, I print out the @pastLines.

Here is a small program that does what you want:


#!/usr/bin/perl -w
use strict;
my @pastLines;
while (<>)
{
shift @pastLines if (@pastLines > 2);
if (m/MATCH/ .. m/^$/)
{
print @pastLines if m/MATCH/;
print $_;
}
push @pastLines, $_;
}
__END__


Now save it to a file (you can call it something like
"2before.pl"), replace the pattern m/MATCH/ with whatever you want
your pattern match to be, and then call it with a command like:

perl 2before.pl input.txt

and you'll see that the lines two before the match all the way to the
blank line are printed.

This also prints the final blank line. If you don't want that
blank line, replace the line:

print $_;

with:

print $_ unless m/^$/;

I hope this helps, Tim.

-- Jean-Luc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Question about my projects 3
newbie question 6
Newbie question 3
CSV confusion newbie question 1
Newbie Question 6
Tasks 1
problems with throw/catch (newbie) 9
newbie question 3

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top