![]() |
|
|
|
|
|||||||
| 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 |
| Building Full-Text Search Applications with Oracle Text | iBot | Oracle Updates (RSS) | 0 | 04-06-2008 02:10 AM |
| How to delete first 5 lines and last five lines in all text files | ragavendran31 | Shell Programming and Scripting | 10 | 02-21-2008 04:58 AM |
| Perl: Search for string on line then search and replace text | Crypto | Shell Programming and Scripting | 4 | 01-04-2008 07:24 AM |
| Moving next 2 lines contents to previous lines | Amruta Pitkar | Shell Programming and Scripting | 8 | 04-26-2007 03:09 AM |
| search and retrieve previous line in file | paulsew | Shell Programming and Scripting | 2 | 02-23-2007 05:04 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
need to search text and output previous lines
I have a file (OMlog0) that is quite large, I need to find the line "Automatic Recharge Audit Process Finished" and output that line and the time stamp that occurs two lines previous. The line that I was using is "sed -n '/Automatic Recharge Audit Process Finished/,/No errors/p' /sn/log/OM* > ARe.out", but it doesn't give me the time stamp just the "Finished" line and the "No Errors" line.
excerpt from OMlog0: +++ MRMCMAS 2008-05-05 09:56:10 ASRT #360578 cc1 LEAD > REPT SPA=EPPSA24D Automatic Recharge Audit Process Finished Correctly! Errors found: No errors END OF REPORT #360578++- +++ MRMCMAS 2008-05-05 09:56:12 ASRT #360579 cc0 ACT > ** REPT NETWORK ASSERT=200, SPA=EPAY24D Network Message Error - Invalid value BILLID in is41css7!t_answer Subscriber ID = 4165551212 Call ID = IS41_1890072211480 Call Instance ID = 594903 Scenario Location=is41css7!t_answer_received Trigger type =is41c_trty_t_answer END OF REPORT #360579++- +++ MRMCMAS 2008-05-05 09:56:12 ASRT #360580 cc0 ACT > ** REPT NETWORK ASSERT=200, SPA=EPAY24D Network Message Error - Invalid value BILLID in is41css7!t_disconnect Subscriber ID = 4165551212 Call ID = IS41_1890072211480 Desired output: +++ MRMCMAS 2008-05-05 09:56:10 ASRT #360578 cc1 LEAD > REPT SPA=EPPSA24D Automatic Recharge Audit Process Finished Correctly! Errors found: No errors |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
You can use 'grep' to print out the line you're looking, the two lines above it, and the line after it:
grep "Automatic Recharge Audit Process Finished Correctly" -B 2 -A 1 file.txt +++ MRMCMAS 2008-05-05 09:56:10 ASRT #360578 cc1 LEAD > REPT SPA=EPPSA24D Automatic Recharge Audit Process Finished Correctly! Errors found: No errors Hope this helps. |
|
#3
|
|||
|
|||
|
Thanks, but it doesn't seem to work for my system.
I am running Solaris 10 am when I do a man grep, the -A and -B options don't exist. when I execute the command that you provided I get the following grep: can't open -B grep: can't open 2 grep: can't open -A grep: can't open 1 grep: can't open file.txt thanks, Al |
|
#4
|
|||
|
|||
|
I don't know Solaris, but if your grep supports the "-p" (paragraph) option you could use that. As it seems from the example the parts of the output you are interested in are delimited by blank lines (which is what defines "paragraphs" in the sense of the "grep -p").
I hope this helps. bakunin |
|
#5
|
|||
|
|||
|
If you do man on grep you will see that B and A are not supported operands
|
|
#6
|
|||
|
|||
|
I really appreciate everyone's help, but it Solaris doesn't seem to like the -p option either. Is there maybe another command either than grep that I should try?
|
|
#7
|
|||
|
|||
|
awk is best for such cases
Do something like this:
awk '/Automatic Recharge Audit Process Finished Correctly/ { \ print pre2 ; print pre1 ;print ;getline ;print ;next}\ { pre1=$0; pre2=pre1}' /sn/log/OM* > ARe.out |
|||
| Google The UNIX and Linux Forums |