The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-18-2008
ammu ammu is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 79
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
  #2 (permalink)  
Old 12-19-2008
FlyingSquirrel FlyingSquirrel is offline
Registered User
  
 

Join Date: May 2008
Posts: 38
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;
}
}
}
  #3 (permalink)  
Old 12-19-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,088
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
  #4 (permalink)  
Old 12-19-2008
prakash.gr prakash.gr is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 32
perl script for dbms_stats package

Hi,

my sorry i wrote wrong wrongly here
Thanks

Prakash

Last edited by prakash.gr; 12-19-2008 at 03:07 AM.. Reason: wrongly entered in this thread
  #5 (permalink)  
Old 12-19-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
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.
  #6 (permalink)  
Old 12-19-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Code:
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;
  #7 (permalink)  
Old 12-22-2008
jatanig jatanig is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 12
Quote:
Originally Posted by ammu View Post
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
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);

Last edited by jatanig; 12-22-2008 at 04:33 AM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0