Finding lines matching the Pattern and their previous lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding lines matching the Pattern and their previous lines in a file
# 1  
Old 07-02-2010
Finding lines matching the Pattern and their previous lines in a file

Hi,

I am trying to locate the occurences of certain pattern like 'Possible network disconnect' in a text file. I can get the actual lines matching the pttern using:

grep -w 'Possible network disconnect' file_name.

But I am more interested in getting the timing of these events which are located on the previous lines of those which match the pattern.

I tried some other techniques like:

while read LINE do
....
....
....
done< file_name

But could not extract the desired lines from the text file.

Can anyone help me accomplish desired results?

Thanks a million in advance.
# 2  
Old 07-02-2010
Adjust parameter after -B, depending on how many lines before match you want to print:
Code:
grep -B5 -w 'Possible network disconnect' file_name

# 3  
Old 07-02-2010
Finding lines matching the Pattern and their previous lines in a file

Hi bartus11,

Thanks for the reply, but my OS doesn't support -B option to grep.

Mine is SunOS 5.8 Generic_117350-45 sun4u sparc SUNW,Sun-Fire-V440

Regards,
Sagar
# 4  
Old 07-02-2010
This will print matching line, then n lines that were before (in random order):
Code:
awk -vn=5 '{a[NR%n]=$0}/Possible network disconnect/{for (i in a) print a[i]}' file_name

# 5  
Old 07-02-2010
This one prints 5 lines before the pattern + the pattern itself:
Code:
awk '/Possible network disconnect/ { print NR }' file_name | while read line
do
awk '{ if(NR >= '$(($line - 5))' && NR <= '$line') print }' file_name
done

# 6  
Old 10-24-2010
Quote:
Originally Posted by bartus11
This will print matching line, then n lines that were before (in random order):
Code:
awk -vn=5 '{a[NR%n]=$0}/Possible network disconnect/{for (i in a) print a[i]}' file_name

@bartus, could you please explain your code ?

a[NR%n] : what does the % stand for : is it the modulo operator ?

when you write
for (i in a) print a[i]
what is the initial i value : 1 ? or 0 ?
# 7  
Old 10-24-2010
Code:
nawk '{A[NR]=$0; delete A[NR-n]}/Possible network disconnect/{for (i=NR-n;i<=NR;i++) if(A[i]) {print A[i]; delete A[i]}}' n=5 infile


Last edited by Scrutinizer; 10-24-2010 at 02:02 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Previous and Post lines in a pattern

Dear Community; I am posting this after looking at several solutions that were not fully relevant to the issue that I am facing. I have a large xml file, 100k+ lines which have patterns like below: <OfferDefinition Id="11"> <Type>Account</Type> ... (3 Replies)
Discussion started by: mystition
3 Replies

2. Shell Programming and Scripting

Want to print out lines with a matching pattern from file

Hi all, I want to search for strings in file1 that can be found in file2 and print out the whole line when matching pattern is found. I have used the below command, but this is not working for me, because it is writing out only the matching patterns from file2, not the whole line. fgrep -o... (2 Replies)
Discussion started by: MonikaB
2 Replies

3. UNIX for Dummies Questions & Answers

FIND matching pattern of lines in a file

I need to search for two patterns in a file and find number of matching lines. find . -type f | xargs grep "DROP TABLE" | wc -l find . -type f | xargs grep "DROP SYNONYM" | wc -l The above code works. However I am looking at finding a commnd that will simplify as on a singe command... (2 Replies)
Discussion started by: Siva SQL
2 Replies

4. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

5. Shell Programming and Scripting

Find pattern a delete previous 5 lines

Hi guys, i have the follow problem i need to delete 10 row before the pattern and 1 after and the pattern row itself. file looks like: frect 9.8438 25.8681 10.625 25 . dynprop \ (# \ (call fox_execute(__self))) \ (FOX_VAR_29 \ ... (4 Replies)
Discussion started by: EjjE
4 Replies

6. Shell Programming and Scripting

pattern matching lines using the date, and then joining the lines

Hi Guys, Was trying to attempt the below using awk and sed, have no luck so far, so any help would be appreciated. Current Text File: The first line has got an "\n", and the second line has got spaces/tabs then the word and "\n" TIME SERVER/CLIENT TEXT... (6 Replies)
Discussion started by: eo29
6 Replies

7. Shell Programming and Scripting

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. (30 Replies)
Discussion started by: namishtiwari
30 Replies

8. Shell Programming and Scripting

Search for a pattern in a file and print previous lines from a particular point

Hi, I am new to ksh scripting and I have a problem. I have a file in which I have to search for a particular pattern say 'a' then from that line I need to search for another pattern say 'b' in the previous lines and thne print the file from pattern 'b' till the end of file. For eg: ... (2 Replies)
Discussion started by: umaislearning
2 Replies

9. Shell Programming and Scripting

delete lines in file matching a pattern

I have a text file, a sample of which is as follows: r/- * 0: WINDOWS/Microsoft.NET/Framework/v2.0.50727/ASP.NETWebAdminFiles/Images/headerGRADIENT_Tall.gif r/- * 0: WINDOWS/SoftwareDistribution/Download/cf8ec753e88561d2ddb53e183dc05c3e/backoff.jpg r/- * 0: ... (2 Replies)
Discussion started by: stumpyuk
2 Replies

10. Shell Programming and Scripting

Reading lines in a file matching a pattern

Hi, I need to redirect the lines in a file to a different file if the character starting from 2 to 6 in the line are numerical . Please let me know if anyone have any script to do this. Thanks, Ranjit (4 Replies)
Discussion started by: torenji
4 Replies
Login or Register to Ask a Question

Featured Tech Videos