Log File - Getting Info about preceding Date of Pattern Found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Log File - Getting Info about preceding Date of Pattern Found
# 1  
Old 07-15-2010
Log File - Getting Info about preceding Date of Pattern Found

Ok

Suppose I have a log file like the below:

Code:
2010-07-15 00:00:01,410 DEBUG
2010-07-15 00:01:01,410 DEBUG
2010-07-15 00:01:02,410 DEBUG com.af ajfajfaf affafadfadfd dfa fdfadfdfadfadf
fafafdfadfdafadfdaffdaffadf
afdfdafdfdafafd error
error failure afdfadfdfdfdf
EBUDGG eafaferror failure agaf@fafa

Now, say i searched the log for the pattern "failure agaf@fafa". How do I get the latest date closest to the found search pattern?

In the case of the above, the latest date closest to the search pattern would be "2010-07-15 00:01:02".

pretty much, I want to be able to know what date the found "pattern" occured. and the way to do that is to associate it with the latest time it is under.

thanks guys.
# 2  
Old 07-15-2010
Code:
sed -n '/^[0-9]\{4\}-[0-1][0-9]-/ { h; }
/agaf@fafa/ { 
x; 
s/^\([0-9]\{4\}-[0-1][0-9]-[^,]*\).*/\1/
p; 
}' infile


Last edited by radoulov; 07-15-2010 at 12:08 PM.. Reason: Optimized.
# 3  
Old 07-15-2010
Quote:
Originally Posted by radoulov
Code:
sed -n '/^[0-9]\{4\}-[0-1][0-9]-/ { 
  s/^\([0-9]\{4\}-[0-1][0-9]-[^,]*\).*/\1/; h; 
  }
/agaf@fafa/ { x; p; }' infile

would this work for all cases? because i would want this to be applied to all cases where pattern errors were found in logs similar to the one i pasted above.
# 4  
Old 07-15-2010
Quote:
Originally Posted by SkySmart
would this work for all cases? because i would want this to be applied to all cases where pattern errors were found in logs similar to the one i pasted above.
I don't know, try it and if it doesn't work ,post a bigger sample of your log file.

By the way, I changed code: it will be more efficient if we substitute only if/when we find a match.
This User Gave Thanks to radoulov For This Post:
# 5  
Old 07-15-2010
Quote:
Originally Posted by radoulov
I don't know, try it and if it doesn't work ,post a bigger sample of your log file.

By the way, I changed code: it will be more efficient if we substitute only if/when we find a match.

thank you so much. it looks like it works. how do you put this into a one liner so it can be easily executed?

I tried this but didn't receive any response back from the script i put it into:

Code:
sed -n '/^[0-9]\{4\}-[0-1][0-9]-/ { h; } /agaf@fafa/ { x; s/^\([0-9]\{4\}-[0-1][0-9]-[^,]*\).*/\1/ p; }' infile

# 6  
Old 07-15-2010
If you want it on a single line, some sed implementations will require something like this:

Code:
sed -n -e '/^[0-9]\{4\}-[0-1][0-9]-/{ h; }' -e '/agaf@fafa/{ x; s/^\([0-9]\{4\}-[0-1][0-9]-[^,]*\).*/\1/p; }' infile

# 7  
Old 07-15-2010
Code:
awk '/^[0-9][0-9]/ {split($2,s,",");a=$1 FS s[1]} 
      /failure agaf@fafa/ {b=a} END{print b}' urfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

3. Shell Programming and Scripting

Copy/print all lines between pattern is found in .log files

Hi, I have a folder with multiple (< 33) .log files. And I have to copy the lines between two patterns from all the .log files to a new file. (script file with a loop?) Thanks in advance. 1.log ... .. xx1> begin ... .. .. >>> Total: 2 Alarms .. .. (17 Replies)
Discussion started by: AK47
17 Replies

4. Shell Programming and Scripting

Getting value of a pattern preceding another pattern

I have a file like this ------------------------------- -------------------------------------- I need a way to find the timestamp preceding the ERR-XXXXX Here XXXX deonoes any any numeric string 00000-99999 that is a output like this 2012-11-12 : 11:59-ERR-XXXXX 2012-11-12 : ... (4 Replies)
Discussion started by: swayam123
4 Replies

5. Shell Programming and Scripting

Need help on appending all the lines in a file after a pattern is found

Hi Friends, I am working on a file which has content as follows Wed,Database,ABC_cube,loaded Wed,Logging,out,user,302002654,active,for,0,minutes Wed,Logging,out,user,109000151,active,for,8,minutes Wed,Logging,out,user,302002654,active,for,0,minutes... (8 Replies)
Discussion started by: dev.devil.1983
8 Replies

6. Shell Programming and Scripting

Count of matched pattern occurences by minute and date in a log file

Anyone knows how to use AWK to achieve the following Sun Feb 12 00:41:01-00:41:59 Success:2 Fail:2 Sun Feb 12 00:42:01-00:42:59 Success:1 Fail:2 Sun Feb 12 01:20:01-01:20:59 Success:1 Fail:2 Mon Feb 13 22:41:01-22:41:59 Success:1 Fail:1 log file: Success Success Fail Fail ... (9 Replies)
Discussion started by: timmywong
9 Replies

7. Shell Programming and Scripting

Extract info from log file and compute using time date stamp

Looking for a shell script or a simple perl script . I am new to scripting and not very good at it . I have 2 directories . One of them holds a text file with list of files in it and the second one is a daily log which shows the file completion time. I need to co-relate both and make a report. ... (0 Replies)
Discussion started by: breez_drew
0 Replies

8. Solaris

Search date pattern in application log file

I am viewing a file in vi editor and would like to search for a date pattern. In the log, the timestamp is enclosed in parentheses ''. I am using the '/' option in vi to search for the pattern. log snippet: 000000f4 ServletWrappe I SRVE0242I: : Initialization successful. 000000f4... (3 Replies)
Discussion started by: Vangogh78
3 Replies

9. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

10. Shell Programming and Scripting

Finding & Moving Oldest File by Parsing/Sorting Date Info in File Names

I'm trying to write a script that will look in an /exports folder for the oldest export file and move it to a /staging folder. "Oldest" in this case is actually determined by date information embedded in the file names themselves. Also, the script should only move a file from /exports to... (6 Replies)
Discussion started by: nikosey
6 Replies
Login or Register to Ask a Question