The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
pattern matching over more lines trek Shell Programming and Scripting 3 04-22-2008 03:37 AM
Script to find file name for non matching pattern sujoy101 Shell Programming and Scripting 5 03-31-2008 06:10 AM
Reading lines in a file matching a pattern torenji Shell Programming and Scripting 4 10-25-2007 01:15 AM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 12:41 PM
getting file words as pattern matching arunkumar_mca High Level Programming 5 05-31-2005 12:28 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-29-2008
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 288
Pattern matching in file and then display 10 lines above every time

hiii,

i have to write a shell script like this----

i have a huge log file name abc.log .i have to search for a pattern name "pattern",it may occur 1000 times in the log file,every time it finds the pattern it should display the 10 lines above the pattern.
I appericiate your help.
Reply With Quote
Forum Sponsor
  #2  
Old 01-29-2008
mirusnet's Avatar
Registered User
 

Join Date: Dec 2007
Posts: 139
Quote:
Originally Posted by namishtiwari View Post
hiii,

i have to write a shell script like this----

i have a huge log file name abc.log .i have to search for a pattern name "pattern",it may occur 1000 times in the log file,every time it finds the pattern it should display the 10 lines above the pattern.
I appericiate your help.

Code:
 grep -B 10 ¨pattern¨ file
Reply With Quote
  #3  
Old 01-29-2008
Registered User
 

Join Date: Aug 2007
Location: Bangalore
Posts: 288
Quote:
Originally Posted by mirusnet View Post
Code:
 grep -B 10 ¨pattern¨ file
It's telling illegal option -B;I am using HP-UX.
Reply With Quote
  #4  
Old 01-29-2008
Registered User
 

Join Date: Mar 2007
Location: Chennai
Posts: 222
Most Linux grep version supports that option,i guess other dont do,

I have the script to do the job,
Code:
#!/bin/ksh
Usage() {
 echo " mygrep pat files.."
 exit
}

(( $# < 2 )) && Usage

Leading=10

PAT=$1
shift;
FileList=$@

for FileName in ${FileList} ; do
  for LineNo in $( grep -n  $PAT $FileName | cut -d":" -f1 )  ; do
     From=$((LineNo-Leading+1))
     sed -n "$From,$LineNo"p $FileName
  done
done
Thanks
Nagarajan G
Reply With Quote
  #5  
Old 01-30-2008
drl's Avatar
drl drl is offline
Registered User
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 556
Hi.

If I understand the problem and the solution from ennstate, then sed will be loaded up 1000 times. Here is an alternate solution. The attachment is a perl script, pvg (perl version of grep, very limited edition). Assuming that you have perl, this will do simple matching and it will also do the "-B n" behavior of GNU grep. This can then be called once to process the log file. (Get the attachment, rename it to "pvg", then run the test script.) Execute "./pvg -h" for a help page.

Here's a test script:
Code:
#!/bin/bash -

# @(#) s3       Demonstrate printing of text before a matched line.

echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1)

P=./pvg

echo
echo " Input file data3:"
cat data3

echo
echo " Looking for d, normal search:"
$P d data3

echo
echo " Looking for d, print previous 2:"
$P -B 2 d data3

echo
echo " Looking for d, print previous 2, separator:"
$P -s '     ----- \n' -B 2 d data3

echo
echo " Looking for ask, print previous 100, quiet:"
$P -q -B 100 ask data3

exit 0
Producing:
Code:
% ./s3
(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0

 Input file data3:
     1  Alabama AL
     2  Alaska AK
     3  Arizona AZ
     4  Arkansas AR
     5  California CA
     6  Colorado CO
     7  Connecticut CT
     8  Delaware DE
     9  District of Columbia DC
    10  Florida FL

 Looking for d, normal search:
     6  Colorado CO
    10  Florida FL
 ( Lines read: 10; hits: 2 )

 Looking for d, print previous 2:
     4  Arkansas AR
     5  California CA
     6  Colorado CO
     8  Delaware DE
     9  District of Columbia DC
    10  Florida FL
 ( Lines read: 10; hits: 2 )

 Looking for d, print previous 2, separator:
     4  Arkansas AR
     5  California CA
     6  Colorado CO
     -----
     8  Delaware DE
     9  District of Columbia DC
    10  Florida FL
 ( Lines read: 10; hits: 2 )

 Looking for ask, print previous 100, quiet:
     1  Alabama AL
     2  Alaska AK
It can be a lot of work to make sure that perl and friends are installed, but once it's done, then you can use it for many things. Most systems have it already available.

There is also cgrep at freshmeat.net: Project details for cgrep which has a wealth of features. You need to go through a compilation, but it was fairly painless.

Best wishes ... cheers, drl
Attached Files
File Type: txt pvg.txt (3.5 KB, 8 views)
Reply With Quote
  #6  
Old 01-30-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
The English module is known to create inefficiencies in perl programs. See the English man page for details: English - perldoc.perl.org

I am not sure if it affects the code you posted drl but you may want to take a look.
Reply With Quote
  #7  
Old 01-30-2008
Registered User
 

Join Date: Jan 2008
Posts: 328
If I was going to use perl I would just use Tie::File, then you can treat the file like an array and use array subscripting to backtrack 10 lines. But he seems to want a shell script so I can't help with that unless calling a perl script is OK.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:54 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0