![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| retrieve lines that match a pattern | fadista | UNIX for Dummies Questions & Answers | 2 | 12-10-2008 04:40 AM |
| Match a pattern and copy above two lines | Danish Shakil | Shell Programming and Scripting | 16 | 08-01-2008 02:28 AM |
| Sed to delete exactly match pattern and print them in other file | new_buddy | Shell Programming and Scripting | 3 | 07-10-2008 03:59 AM |
| How to delete lines do NOT match a pattern | JumboGeng | UNIX for Dummies Questions & Answers | 1 | 09-20-2006 06:52 AM |
| match a pattern, print it and the next line | nymus7 | Shell Programming and Scripting | 4 | 07-29-2005 01:59 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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 ($. < $c) { print $_; } else { last; } } } |
|
||||
|
Hi, below package contains serverl method, 'getLinesAfterString' may address your issue.
Code:
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
|
|
||||
|
Quote:
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); Last edited by jatanig; 12-22-2008 at 04:33 AM.. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|