extracting lates pattern match from multiple matches in log


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers extracting lates pattern match from multiple matches in log
# 1  
Old 11-26-2011
extracting lates pattern match from multiple matches in log

Hi,

I have a large, multiline log file.

I have used pcregrep to extract all entries in that log that match a particular pattern - where that pattern spans multiple lines.

However, because the log file is large, and these entries occur every few minutes, I still output a very large amount of data.

All I am actually looking for is the last occurrence of the patter match in the log. Grep allows me to specify number of matches, but only works on single lines. pcregrep allows multiple lines, but does not limit matches.

I guess the ideal solution is a way of running a regex match from the base of the log upwards, and stopping at the first full match.

Can someone suggest a sensible solution?

Cheers,

Ben
# 2  
Old 11-26-2011
Can you show us example of input file and expected output?
# 3  
Old 11-26-2011
Theres quite a lot of it... but summarizing:

<timestamp> checkStatus SUCCESS [
many lines of varied text
]

All other lines of the log will not match the above, and will be of the form:

<timestamp> <some other call> .......[

]
I successfully matched this pattern, but it matches every entry - and there are a few hundred in the current log. I have piped them all to a file, but still extracting the last match is a problem...

What I am trying to extract is the most recent match of the pattern - so that nearest the base of the file.

---------- Post updated at 11:01 AM ---------- Previous update was at 09:45 AM ----------

I have sort of achieved what I need...

tail -n500 <myLog>.log | sed -n '/^.*checkStatus SUCCESS/,/]/ p'

This gives me the output I want, but has two limitations:

If the entry, for whatever reason, does not appear in the arbitrarily chosen last 500 lines of the log, then no match is returned

If it appears twice, then it is returned twice...

Last edited by dbrb2; 11-26-2011 at 12:27 PM..
# 4  
Old 11-26-2011
Try:
Code:
perl -ln0e '/(checkStatus SUCCESS.*?\]).*?$/s;print "$1\n"' mcsystemstatusmanager.log

Didn't test it though.
# 5  
Old 11-26-2011
A definite improvement, in that it returns only one match - however, it starts the match from the start of the log file, so I end up with the first, not the last match...
# 6  
Old 11-26-2011
OK, try this:
Code:
perl -ln0e '@a=/(checkStatus SUCCESS.*?\])/gs;print "$1\n"' mcsystemstatusmanager.log

This time I've tested it :P
# 7  
Old 11-26-2011
Yes - that has done it - thanks!

Can I ask you eactly what it is doing though? They key changes seem to be the addition of the /g flag, and the @a= at the start of the regex...

Ben
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Egrep patterns in a file and limit number of matches to print for each pattern match

Hi I need to egrep patterns in a file and limit number of matches to print for each matched pattern. -m10 option is not working out in my sun solaris 5.10 Please guide me the options to achieve. if i do head -10 , i wont be getting all pattern match results as output since for a... (10 Replies)
Discussion started by: ananan
10 Replies

3. Shell Programming and Scripting

Remove multiple lines that match pattern

Not sure how I can accomplish this. I would like to remove all interfaces that have the commands I would like to see: switchport port-security, spanning-tree portfast. One line is no problem. interface FastEthernet0/8 spanning-tree portfast interface FastEthernet0/9 spanning-tree... (4 Replies)
Discussion started by: mrlayance
4 Replies

4. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

5. UNIX for Dummies Questions & Answers

Extracting range of characters if pattern matches

Im trying compare values between files and if they match I want to extract some characters in between those values for many files. They are in two directories and have the name filename but one ends in .enr. They look like this. cat bat.1.enr name,start,end bat.1,231, 234 and another... (5 Replies)
Discussion started by: verse123
5 Replies

6. Shell Programming and Scripting

grep - match files containing minimum number of pattern matches

I want to search a bunch of files and list only those containing a minimum number of pattern matches. So if I want to identify files containing 3 (or more) instances of the pattern "said:" and I have file1 that contains the lines: He said: She said: and file2 that contains the lines: He... (3 Replies)
Discussion started by: stumpyuk
3 Replies

7. Shell Programming and Scripting

BASH: extracting values from multiple lines after a match

Hi there, I have the following output, # raidctl -l RAID Volume RAID RAID Disk Volume Type Status Disk Status ------------------------------------------------------ c0t1d0 IM OK c0t1d0 OK ... (4 Replies)
Discussion started by: rethink
4 Replies

8. Shell Programming and Scripting

Extracting N lines match number X of a pattern

Hi All, as the title says I need to extract N lines after match number X of a pattern. e.g. 111 xxx xxx 111 yyy yyy 111 www www 111 zzz zzz I would like to extract the two lines after the second 111 occurrence. I tried with grep but I didn't find any options to do that. Any... (11 Replies)
Discussion started by: f_o_555
11 Replies

9. Shell Programming and Scripting

help extracting a matching pattern and next lines of match

Hi there, i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><!]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID>... (6 Replies)
Discussion started by: vicious
6 Replies

10. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies
Login or Register to Ask a Question