How to get lines before and after a searched text?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get lines before and after a searched text?
# 1  
Old 06-28-2012
How to get lines before and after a searched text?

Hi,
I am trying to monitor alert log of oracle. i am searching based on ORA- error message
if i find the line, i want the 2 to 3 lines before search line and 2 to 3 lines after the searched line.
like for example, oracle has generated ORA7445 errors in the alert log, when i search for that line, i want lines above that message line and after this message line.
How can i achieve this, please do help me out.

Code:
Tue Jun 12 07:25:51 2012
Archived Log entry 296412 added for thread 1 sequence 37385 ID 0x6deec879 dest 1:
Tue Jun 12 07:29:44 2012
Completed checkpoint up to RBA [0x920a.2.10], SCN: 22243048084
Tue Jun 12 07:30:14 2012
Exception [type: SIGSEGV, Address not mapped to object] [ADDR:0xB38F0000000073] [PC:0x1085CD368, kxhrInit()+136] [flags: 0x0, count: 1]
Errors in file /apps/oracle/admin/diag/rdbms/hrprd89/HRPRD89/trace/HRPRD89_ora_864458.trc  (incident=42273):
ORA-07445: exception encountered: core dump [kxhrInit()+136] [SIGSEGV] [ADDR:0xB38F0000000073] [PC:0x1085CD368] [Address not mapped to object] []
Incident details in: /apps/oracle/admin/diag/rdbms/hrprd89/HRPRD89/incident/incdir_42273/HRPRD89_ora_864458_i42273.trc
Use ADRCI or Support Workbench to package the incident.
See Note 411.1 at My Oracle Support for error and packaging details.
Tue Jun 12 07:30:18 2012
Dumping diagnostic data in directory=[cdmp_20120612073018], requested by (instance=1, osid=864458), summary=[incident=42273].
Tue Jun 12 07:30:18 2012
Sweep [inc][42273]: completed
Sweep [inc2][42273]: completed


Moderator's Comments:
Mod Comment Please use code tags, thanks!

Last edited by zaxxon; 06-28-2012 at 07:40 AM.. Reason: code tags
# 2  
Old 06-28-2012
Could this help you ?

Code:
grep -A3 -B3 "ORA-07445" Filename

# 3  
Old 06-28-2012
thank u very much for a fast response, but it is failing,
this is on aix. anythoughts how else i can do this

Code:
[HRPRD89]$ grep -A3 -B3 "ORA-07445" alert_HRPRD89.log
grep: illegal option -- A
grep: illegal option -- 3
grep: illegal option -- B
grep: illegal option -- 3
usage: grep [-r] [-R] [-H] [-L] [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] -e pattern_list...
        [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] [-e pattern_list...]
        -f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]] pattern_list [file...]


Last edited by methyl; 06-28-2012 at 10:34 AM.. Reason: please use code tags
# 4  
Old 06-28-2012
How about this ?
Code:
    awk '{if(/ORA-07445/){flg=1;k=j-2;for(l=k;l<=j;l++){print a[l]} k=0;j=0; print $0}else if (flg == 1 && i<3) {print; ++i}else{a[++j]=$0;i=0;flg=0}}' Filename

# 5  
Old 06-28-2012
Alternatively:
Code:
awk '/^... ... .. ..:..:/{if(p~s)print p; p=$0; next}{p=p ORS $0} END{if(p~s)print p}' s="ORA-07445" infile

or

Code:
awk '/^... ... .. ..:..:/{printf RS}1' infile | awk '/ORA-07445/' RS=


Last edited by Scrutinizer; 06-28-2012 at 09:58 AM..
# 6  
Old 06-28-2012
Thanks all for helping,

both the solutions are working, sorry asking too much,
is there any way i can get line numbers too along with the output,
Also is there any way i can search based on a certain day.
The reason being. the alert log bloat out and it contains many days of data,
i am planning to run a daily report which collects todays information.
is there anyway i can do this ?
Again sorry i am asking too much here.
# 7  
Old 06-28-2012
Try:
Code:
awk '/^... ... .. ..:..:/{printf RS}{print NR ": " $0}' infile | awk '/ORA-07445/' RS=


plus date:
Code:
awk '/^... ... .. ..:..:/{printf RS}{print NR ": " $0}' infile | awk '/Jun 12 .* 2012.*ORA-07445/' RS=


Last edited by Scrutinizer; 06-28-2012 at 11:08 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert text after the first occurance of searched string entry in a file

My server xml file has huge data part of which i'm sharing below. I wish to add the below text held by variable "addthisline" after the closing braces i.e --> once the first </Connector> tag is found. addthisline="I need to be inserted after the comments" Thus my searchstring is... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

3. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

5. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

6. Shell Programming and Scripting

sed show lines text between 2 blank lines

I have a file like blah blah blah blah this is the text I need, which might be between 1-4 lines, but always has a blank line above and below it, and is at the end of the text file the code tags don't show the trailing blank line. I started by deleting the last blank line with: ... (2 Replies)
Discussion started by: unclecameron
2 Replies

7. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

8. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

9. Shell Programming and Scripting

how to get lines prior to the line being searched

Hi, Can anbody please let me know how i can retrieve lines above the line being searched in a file. I am looking for an error message from a file, if I see that message I want the lines above that message along with this line. how do we do this. Please do let me know An example which i have... (2 Replies)
Discussion started by: arunrao_oradba
2 Replies

10. Shell Programming and Scripting

How to delete first 5 lines and last five lines in all text files

Hi I want to delete first five and last five lines in text files without opening the file and also i want to keep the same file name for all the files. Thanks in advance!!! Ragav (10 Replies)
Discussion started by: ragavendran31
10 Replies
Login or Register to Ask a Question