in reply to Re^5: How to print the lines immediately above and below a matching line?
in thread How to print the lines immediately above and below a matching line?
Here is the code with the change
use strict; use warnings; open( my $fh, '<', 'input.txt') or die "Error opening file - $!\n"; open OUT, ">", "output.txt" or die "could not open output.txt $! \n"; my $this_line = ""; my $do_next = 0; my $data = <DATA>; while(<$fh>) { my $last_line = $this_line; $this_line = $_; chomp $data; if ($this_line =~ /\Q$data/) { print OUT $last_line unless $do_next; print OUT $this_line; $do_next = 1; } else { print OUT $this_line if $do_next; $last_line = ""; $do_next = 0; } } close ($fh); __DATA__ 4386_7#8 4350_7#6 4414_1#6
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^7: How to print the lines immediately above and below a matching line?
by Anonymous Monk on Nov 25, 2012 at 14:32 UTC | |
by MB123 (Initiate) on Nov 25, 2012 at 18:41 UTC | |
by Anonymous Monk on Nov 25, 2012 at 18:45 UTC |
In Section
Seekers of Perl Wisdom