need to search text and output previous lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need to search text and output previous lines
# 1  
Old 05-05-2008
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
# 2  
Old 05-05-2008
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  
Old 05-06-2008
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  
Old 05-06-2008
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  
Old 05-06-2008
If you do man on grep you will see that B and A are not supported operands
# 6  
Old 05-06-2008
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  
Old 05-07-2008
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
This User Gave Thanks to baruchgu For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

(n)awk: print regex search output lines in one line

Hello. I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas. I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them... (2 Replies)
Discussion started by: Tobias-Reiper
2 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

E-mail Output Merge Lines for Some Text..

Hi all. This is the content of the text file used for the e-mail: TM ICP-EDW BILLING REGISTER USAGE BREAKDOWN_01062014.csv TM_ICP_EDWH_FICL_13062014.TXT TM_ICP_EDWH_FICL_16062014.TXT TM_ICP_EDW_Detailed Payment Journal Report_13062014.txt TM_ICP_EDW_Detailed Payment Journal... (9 Replies)
Discussion started by: aimy
9 Replies

4. Shell Programming and Scripting

Remove previous line if next & previous lines have same 4th character.

I want to remove commands having no output. In below text file. bash-3.2$ cat abc_do_it.txt grpg10so>show trunk group all status grpg11so>show trunk group all status grpg12so>show trunk group all status GCPKNYAIGT73IMO 1440 1345 0 0 94 0 0 INSERVICE 93% 0%... (4 Replies)
Discussion started by: Raza Ali
4 Replies

5. Shell Programming and Scripting

Extract two lines before and after the 'search text'

Hi Guys, I have a situation wherein I need to extract two lines from below the search string. Eg. Current: $ grep "$(date +'%a %b %e')" alert.log Mon Apr 12 03:58:10 2010 Mon Apr 12 12:51:48 2010 $ Here I would like the display to be something like Mon Apr 12... (6 Replies)
Discussion started by: geetap
6 Replies

6. Shell Programming and Scripting

Append specific lines to a previous line based on sequential search criteria

I'll try explain this as best I can. Let me know if it is not clear. I have large text files that contain data as such: 143593502 09-08-20 09:02:13 xxxxxxxxxxx xxxxxxxxxxx 09-08-20 09:02:11 N line 1 test line 2 test line 3 test 143593503 09-08-20 09:02:13... (3 Replies)
Discussion started by: jesse
3 Replies

7. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 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

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

10. Shell Programming and Scripting

Add text before lines in command output

Hi2all, I have following command in IBM HMC console: lssyscfg -r prof -m Server-9117-MMA-SN655D350 -F lpar_name,min_mem,desired_mem --header which gives me the following output: lpar_name,min_mem,desired_mem lpar1,1024,2048 lpar2,1024,2048 lpar3,2048,4096 What I want is to add in... (3 Replies)
Discussion started by: UsRb
3 Replies
Login or Register to Ask a Question