search file between last occurence of 2 strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search file between last occurence of 2 strings
# 1  
Old 12-29-2008
search file between last occurence of 2 strings

I need to extract the last block of /== START OF SQLPLUS ==/ and /== END OF SQLPLUS ==/. The logifle is written to several times in a day using >> to append. I need a solution using grep/sed.


logfile looks like this

START OF LOGFILE
/== START OF SQLPLUS ==/

ERROR

/== END OF SQLPLUS ==/

/== START OF SQLPLUS ==/

OK

/== END OF SQLPLUS ==/

/== START OF SQLPLUS ==/

ERROR

/== END OF SQLPLUS ==/

/== START OF SQLPLUS ==/

OK

/== END OF SQLPLUS ==/
END OF LOGFILE


thanks

Last edited by hanton; 12-29-2008 at 03:47 PM..
# 2  
Old 12-29-2008
one way with awk (or nawk on Solaris)-
Code:
awk '{
       if(index($0, "/== START OF SQLPLUS ==/")>0 ){start=1}
       if(index($0, "/== END OF SQLPLUS ==/" )>0 ) {start=0}
       if(start>0 ){
       	if (start==1) {
       	   rec=$0
       	}
       	else {
       	   rec=sprintf("%s\n%s", rec, $0)
       	}
       	start++
       }
       next
       }
       END {print rec} ' inputlogfile

# 3  
Old 12-29-2008
Quote:
Originally Posted by jim mcnamara
one way with awk (or nawk on Solaris)-
Code:
awk '{
       if(index($0, "/== START OF SQLPLUS ==/")>0 ){start=1}
       if(index($0, "/== END OF SQLPLUS ==/" )>0 ) {start=0}
       if(start>0 ){
       	if (start==1) {
       	   rec=$0
       	}
       	else {
       	   rec=sprintf("%s\n%s", rec, $0)
       	}
       	start++
       }
       next
       }
       END {print rec} ' inputlogfile

great tested and workls! now can I output the results to a variable in my ksh script then cat grep for the word ERROR ?

Last edited by hanton; 12-29-2008 at 05:06 PM..
# 4  
Old 12-29-2008
Hi,

just for fun in a sed one-liner:

Code:
sed -n '/== START/,/== END/{/START/{h;D};H};${x;p}' logfile

# 5  
Old 12-30-2008
Quote:
Originally Posted by Christoph Spohr
Hi,

just for fun in a sed one-liner:

Code:
sed -n '/== START/,/== END/{/START/{h;D};H};${x;p}' logfile

tried to run this and got

sed: command garbled: /== START/,/== END/{/START/{h;D};H};${x;p}

Last edited by hanton; 12-30-2008 at 11:17 AM..
# 6  
Old 12-30-2008
Probably your version of sed doesn't understand the ";" shortcut.
So save the following line in a file called sedfile.

Code:
/== START/,/== END/{
    /START/{
    h
    D}
H}
${
x
p
}

Now run the code:

Code:
sed -n -f sedfile logfile

HTH Chris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a text between two strings in a file using regex

Here is my sample file data: My requirement is to have a regex expression that is able to search for visible starting string "SSLInsecureRenegotiation Off" between strings "<VirtualHost " and "</VirtualHost>". In the sample data two lines should be matched. Below is what I tried but... (5 Replies)
Discussion started by: mohtashims
5 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

Search strings from array in second file

I have a file search_strings.txt filled with search strings which have a blank in between and look like this: S. g. Erh. o. J. v. d. Chijs g. Ehr.I would like to search the strings in the second given Textfile.txt and it shall return the column number. Can anybody help with the correct... (3 Replies)
Discussion started by: sdf
3 Replies

4. UNIX for Dummies Questions & Answers

Search specific pattern in file and return number of occurence

Hi I want to search for a specific pattern in file Say ABC;HELLO_UNIX_WORLD;PQR ABC;HELLO_UNIX_WORLD_IS_NOT_ENOUGH;XYZ ABC;HELLO_UNIX_FORUM;LMN Pattern to search is : "HELLO_UNIX_*****" and not "HELLO_UNIX_***_***_" I mean after "HELLO_UNIX" there can only be one word.In this case... (2 Replies)
Discussion started by: dashing201
2 Replies

5. Shell Programming and Scripting

Search multiple Strings in a File

Hi I want to search multiple strings in a file . But the search should start with "From" Keyword and end with before "Where" keyword. Please suggest me. Thanks (2 Replies)
Discussion started by: sboss
2 Replies

6. Shell Programming and Scripting

Search file for pattern2 starting from occurence of pattern1

Hi folks, I have a file which contains several occurences of 2 different patterns. I need to find out the line of first occurence of pattern2 starting after the position of first occurence of pattern1. example file: aaaa pattern2 bbbb pattern1 ccc pattern2 ddd pattern1 eee pattern2... (9 Replies)
Discussion started by: sameucho
9 Replies

7. Shell Programming and Scripting

Search complicated strings on file

Can someone help me? I been figuring out how I can search and extract a complicated search string from a file. The whole string is delimited by a period. And the file where I'm searching is composed of differnt string such as that. For example, I have this search string: and I have a file... (3 Replies)
Discussion started by: Orbix
3 Replies

8. UNIX for Dummies Questions & Answers

How to search unique occurence in a file?

Hi, I have to search and count unique occurence of DE numbers in bold below in a file which has content like below. Proc Tran F-BUY Item Tkey Q5JV Item Tsid JTIZ9 Item Tdat 20091001 Item Tset 20091001 Item Tbkr 5 Item Tshs 2 Item Tprc 897.0 Item Tcom 2000.0 Item Tcm1 20091001... (6 Replies)
Discussion started by: akash028
6 Replies

9. Shell Programming and Scripting

How to search multiple strings in a file

Hi All, I want to search all the ksh scripts that has following details. 1. Search for "exit 0" 2. Search for "sqlldr" or sqlplus" 3. In the above files i want to search for all the script that has no "case" in it. Please advice. Thanks, Deep (2 Replies)
Discussion started by: deepakpv
2 Replies

10. Shell Programming and Scripting

Search for strings & copy to new file

Hi All, I am just learning shell programming, I need to do the following in my shell script. Search a given log file for two\more strings. If the the two\more strings are found then write it to a outputfile else if only one of the string is found, write the found string in one output... (2 Replies)
Discussion started by: amitrajvarma
2 Replies
Login or Register to Ask a Question