You mention wanting to print the lines above and below a matching line, but your code prints the matching line, too. In case you wanted to print all (two or) three lines, consider the following that you can adapt for files:
use strict;
use warnings;
my ( $prevLine, $nextLine );
for ( ; ; ) {
last if eof DATA;
chomp( my $currLine = defined $nextLine ? $nextLine : <DATA> );
if ( $currLine =~ /match this/ ) {
print '-' x 25, "\n";
chomp( $nextLine = <DATA> ) if !eof DATA;
print $prevLine, "\n" if defined $prevLine;
print $currLine, "\n";
print $nextLine, "\n" if defined $nextLine;
print '-' x 25, "\n";
}
else {
undef $nextLine;
}
$prevLine = $currLine;
}
__DATA__
The first line match this
Not this
abcdefg
The one above
Another match this 1
Another match this 2
the one below match this 2
zxcvbnn
Another match this blank above
Second to the last line
The last line match this
Output:
-------------------------
The first line match this
Not this
-------------------------
-------------------------
The one above
Another match this 1
Another match this 2
-------------------------
-------------------------
Another match this 1
Another match this 2
the one below match this 2
-------------------------
-------------------------
Another match this 2
the one below match this 2
zxcvbnn
-------------------------
-------------------------
Another match this blank above
Second to the last line
-------------------------
-------------------------
Second to the last line
The last line match this
-------------------------
The dashes are printed to show the desired output. If the first or last line is a match, only two lines are printed. If you only want the lines above and below a matching line, delete print $currLine, "\n";.
Hope this helps!
Addition: If you want to avoid printing the same line more than once--like in the example above--and have output that more closely resembles grepping the file, you can do the following:
use strict;
use warnings;
my ( $prevLine, $nextLine, %printed );
for ( ; ; ) {
last if eof DATA;
chomp( my $currLine = defined $nextLine ? $nextLine : <DATA> );
if ( $currLine =~ /match this/ ) {
chomp( $nextLine = <DATA> ) if !eof DATA;
print $prevLine, "\n" if defined $prevLine and !$printed{$prev
+Line}++;
print $currLine, "\n" if !$printed{$currLine}++;
print $nextLine, "\n" if defined $nextLine and !$printed{$next
+Line}++;
}
else {
undef $nextLine;
}
$prevLine = $currLine;
}
__DATA__
The first line match this
Not this
abcdefg
The one above
Another match this 1
Another match this 2
the one below match this 2
zxcvbnn
Another match this blank above
Second to the last line
The last line match this
Output:
The first line match this
Not this
The one above
Another match this 1
Another match this 2
the one below match this 2
zxcvbnn
Another match this blank above
Second to the last line
The last line match this
This uses a hash to keep track of previously printed lines, so they're shown only once.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|