in reply to how to read multiple line from a file
Input file data.txt:use strict; use warnings; my $csize = 2; ### Number of lines to save my (@cache, $str, $c); my $file = 'data.txt'; open (IN, $file) or die "Can't open $file for read."; while (<IN>) { shift @cache if $#cache == $csize - 1; push @cache, $_; $c++; $str = join '', @cache; if ($str =~ /jumped\nover/) { print "String found in $file at lines " . ($c - $#cache) . "-$ +c:\n"; print $str; last; } }
Output:the quick brown fox jumped over the unfortunate dog
String found in data.txt at lines 5-6: jumped over
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to read multiple line from a file
by educated_foo (Vicar) on Apr 13, 2013 at 13:27 UTC | |
by LanX (Saint) on Apr 13, 2013 at 14:01 UTC | |
by educated_foo (Vicar) on Apr 14, 2013 at 18:11 UTC | |
by LanX (Saint) on Apr 14, 2013 at 19:43 UTC | |
by educated_foo (Vicar) on Apr 14, 2013 at 20:56 UTC | |
| |
by ww (Archbishop) on Apr 14, 2013 at 00:13 UTC | |
by educated_foo (Vicar) on Apr 14, 2013 at 02:16 UTC | |
|
Re^2: how to read multiple line from a file
by skyworld_chen (Acolyte) on Apr 13, 2013 at 11:59 UTC |