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?
# 15  
Old 06-29-2012
How to get lines before and after a searched text?

Hi Scrut,

Great ..

I need to understand your logic / code. If you have time can you share?

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

Code:
awk '/^... ... .. ..:..:/{printf RS}{print NR ": " $0}' infile | awk '/ORA-07445/' RS=

Thanks
# 16  
Old 06-29-2012
Hi, both methods attempt to create records that consist of a single log entries. Log entries start with a pattern of a date (3 characters, a space, 3 characters, a space, 2 characters, etcetera) such a pattern could be specified more precisely, but it is good to only use the precision that is required, and this will probably do...

Then once we have the start of the record we need to determine the end.

In the first method the end is signified by the start of the new log entry or when the end of the file is reached and the log entry is contained in the variable "p". If it matches "s" then it should be printed.

In the second method an empty line is created above the start of the log entry, so that the second awk can filter these entries by using the special case value of an empty record separator, which splits records if two consecutive newline characters are found...

I hope this helps,

S.
# 17  
Old 06-29-2012
How to get lines before and after a searched text?

Hi Scrut,
Thanks a lot
Code:
awk '/^... ... .. ..:..:/
             {
     line1            if(p~s)
     line2            print p; 
     line3            p=$0; 
     line4            next
              }
                 
               {
                    p=p ORS $0
                
                } 
          END
             {
                if(p~s)  print p}' s="ORA-07445" infile

Please confirm if line 1-4 is if block. This means when if condition fails then control go to
Code:
 {
                    p=p ORS $0
                
                }

# 18  
Old 06-29-2012
That's right.
# 19  
Old 06-29-2012
Many thanks....
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