Perl script to match a pattern and print lines

Hi
I have a file (say 'file1')and I want to search for a first occurence of pattern (say 'ERROR') and print ten lines in the file below pattern. I have to code it in PERL and I am using Solaris 5.9.

I appreciate any help with code

Thanks
Ammu

ammu,

Try this script, if it were called... myscript.pl
chmod 755 myscript.pl
cat file1 | ./myscript.pl

#!/usr/bin/env perl

$p=0;
$c=0;
while (<STDIN>) {
if ($p == 0 && m/ERROR/) {
$p=1;
$c=$. + 10;
}

if \($p == 1\) \{                                                                                                                      
    if \($. &lt; $c\) \{                                                                                                                  
        print $_;                                                                                                                   
    \} else \{                                                                                                                        
        last;                                                                                                                       
    \}                                                                                                                               
\}                                                                                                                                   

}

Hi, below package contains serverl method, 'getLinesAfterString' may address your issue.

package LeoFile;
sub new{
	return bless {};
}
sub _open{
	my $file=shift;
	open FH,"<$file";
}
sub _close{
	close FH;
}
sub _checkPattern{
	my($ref,$pat)=(@_);
	@tmp=@{$ref};
	print "@{$ref}" if($matched==1);
}	
sub getLinesAfterString{
	shift;
	my($file,$str,$line)=(@_);
	_open($file);
	my $cnt;
	while(<FH>){
		$flag=1 if(m/$str/);
		if($flag && $cnt<$line){
			print $_;
			$cnt++;
		}
		else{
			$cnt=0;
			$flag=0;
			next;			
		}
	}
	_close;
}
sub getLinesBetweenString{
	shift;
	my($file,$str1,$str2)=(@_);
	_open($file);
	while(<FH>){
		$flag=1 if(m/$str1/);
		print if ($flag==1);
		$flag=0 if(m/$str2/);
	}
	_close;
}
sub getLinesBetweenStringContainPattern{
	shift;
	my($file,$str1,$str2,$pat)=(@_);
	_open($file);
	while(<FH>){
		$flag=1 if(m/$str1/);
		push @arr,$_ if($flag==1);
		$matched=1 if(m/$pat/);
		if(m/$str2/){
			$flag=0;
			_checkPattern(\@arr,$matched);
			undef @arr;
			$matched=0;
		}
	}
	_close;
}
1
	

Hi,

my sorry i wrote wrong wrongly here
Thanks

Prakash

You can use Tie::File and access the file like an array so its easy to jump lines in a file since you can use the line numbers as the array index.

open (IN, 'file1') or die "$!";
while (<IN>) { 
   if (m/ERROR/) { 
      for (1..10) {
         <IN>;#skips 10 lines
      }
      print;
      last;#stops the "while" loop
   }
}
close IN;

One more Solution:

open(FH, "<file1");
@a=<FH>;
$b=$#a;
for ($n=0;$n<$b;$n++)
{

 if \($a[$n]=~/ERROR/\)
    \{
    foreach $_ \( @a[$n..\($n\+10\)]\)
    \{
    print $_;
    \}
    \}
 \}
  close\(FH\);