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
pattern matching over more lines trek Shell Programming and Scripting 3 04-22-2008 06:37 AM
Script to find file name for non matching pattern sujoy101 Shell Programming and Scripting 5 03-31-2008 09:10 AM
Reading lines in a file matching a pattern torenji Shell Programming and Scripting 4 10-25-2007 04:15 AM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 03:41 PM
getting file words as pattern matching arunkumar_mca High Level Programming 5 05-31-2005 03:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-29-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
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.
  #2 (permalink)  
Old 01-29-2008
mirusnet's Avatar
mirusnet mirusnet is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 146
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
  #3 (permalink)  
Old 01-29-2008
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Quote:
Originally Posted by mirusnet View Post
Code:
 grep -B 10 ¨pattern¨ file
It's telling illegal option -B;I am using HP-UX.
  #4 (permalink)  
Old 01-29-2008
ennstate ennstate is offline
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
  #5 (permalink)  
Old 01-30-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2007
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 704
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, 39 views)
  #6 (permalink)  
Old 01-30-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

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

Join Date: Jan 2008
Posts: 731
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.
Sponsored Links
Closed Thread

Bookmarks

Tags
linux, perl, perl shift, shift, shift perl

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 12:21 AM.


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