![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Quote:
Code:
grep -B 10 ¨pattern¨ file |
|
#3
|
|||
|
|||
|
It's telling illegal option -B;I am using HP-UX.
|
|
#4
|
|||
|
|||
|
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
Nagarajan G |
|
#5
|
||||
|
||||
|
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 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
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 |
|
#6
|
|||
|
|||
|
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
|
|||
|
|||
|
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.
|
|||
| Google The UNIX and Linux Forums |