If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Extracting patterns from a log file

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-29-08, 08:24
gohappy gohappy is offline
Registered User
 
Join Date: Apr 2008
Posts: 24
Question Extracting patterns from a log file

I want to extract Date,Time and Error (ORA-) from error log file as given below.

Sun Apr 28 11:56:56 2008
ORA-01555 caused by SQL statement below (Query Duration=26748 sec)
Wed Apr 29 00:07:33 2008
Completed checkpoint up to ABC [0x15295.2.10], SCN: 0x089e.b4b72add
Wed Apr 29 00:09:48 2008
Beginning log switch checkpoint up to ABC [0x15296.2.10], SCN: 0x045489e.b62bsdd10f6
Wed Apr 29 02:51:58 2008
ORA-000060: Deadlock detected. More info in file /dborafiles/abc/QCDE/admin/udump/aaa_ora_20389.trc.
Wed Apr 29 03:08:32 2008
ABBRC1: Evaluating archive log 3 thread 1 sequence 86703

Need to extract errors(ORA-) and date,time in daily basis.
I cann't use "grep -A/B" as using Solaris-9.

Pls suggest me.

Thanks,
gohappy
Reply With Quote
  #2 (permalink)  
Old 04-29-08, 10:02
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Code:
awk '{ if ($1 ~ /^ORA-/) { print $prev_line" "$0; } $prev_line=$0; }' log_file
You can tighten up the code a bit if you want but it works fine with the example file you supply. You'll need to change log_file to be the name of your log file.

Mike
Reply With Quote
  #3 (permalink)  
Old 04-29-08, 10:14
gohappy gohappy is offline
Registered User
 
Join Date: Apr 2008
Posts: 24
Hi Mike,

Thanks a lot. It's working perfect. Could u pls explain how the script finds today's date.

Thanks,
gohappy
Reply With Quote
  #4 (permalink)  
Old 04-29-08, 10:30
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
it doesn't know anything about today's date!

Code:
awk automatically reads each line into $0

      if $0 starts with ORA- then
            print the previous line ($prev_line) and the current line.

      set $prev_line = current line
It's quite simple logic but I should of laid it out better. I should also of initialised $prev_line but I'll leave that up to you if you wish (google for awk BEGIN).

Mike
Reply With Quote
  #5 (permalink)  
Old 04-29-08, 11:40
gohappy gohappy is offline
Registered User
 
Join Date: Apr 2008
Posts: 24
Thanks Mike for your helping hand ......actually I have to extract the ORA- error datewise.....the log_file contents many errors with old dates and it's updated everyday ....... I have to run the script every day so that it extracts the ORA- errors for the current date......
Reply With Quote
  #6 (permalink)  
Old 04-29-08, 12:17
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Depends on the size of your log file - if it's medium size then I'd just run the above and then grep the output for the current date. If it's a very very large file then you could tail the last 10000 lines and then run as above, greping for the current date again.

Mike
Reply With Quote
  #7 (permalink)  
Old 04-29-08, 12:30
gohappy gohappy is offline
Registered User
 
Join Date: Apr 2008
Posts: 24
That's true......but it is difficult to grep both ORA- and current date combilely.If I put tail -1000f then it shows many old dated errors.As I am having 17 different log_files and have to extract the errors (ORA-) from each file. Trying to extract the error datewise means when I will run the script it will show the ORA- for current day. I have to make a daily Error Report for all 17 the log_files.

Can we put a current date as input to this script so that it will find only today's ORA- errors .

Thanks,
gohappy
Reply With Quote
  #8 (permalink)  
Old 04-30-08, 04:25
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quite possibly - here's a good tutorial on awk
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On