Getting the lines between last occurrence of two patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting the lines between last occurrence of two patterns
# 1  
Old 02-14-2008
Getting the lines between last occurrence of two patterns

Hi
I am new shell scripting, i need help on the follwoing
I have a application log file, the application is called on cron, the log includes a "started" and "finished" lines repeatedly. I need to get the log of the for the latest run of the application.
Sample log file
Started
adfa
fadfa
Finished
Started
asdlkfjal
asfdslaj
asdfaf
afda
Finished
Started
alsfda
Finished
In this log how to do i get the lines between the last occurence of Started and Finished
# 2  
Old 02-14-2008
Try this

tail +`grep -in "Started" <logFile> | tail -1 | awk -F: '{print $1}'` <logFile>

cheers
maverix
# 3  
Old 02-14-2008
thank you very much
it is working fine
Can you explain your script for my understanding
# 4  
Old 02-14-2008
good that you found it useful!!!

check out the man pages for tail, awk and grep to understand how it works.
In particular, check the switches I've used for each command.

Don't think I am being rude to you.
You must have heard the good old story where the fisherman taught a starving person to fish, rather than giving him few of his catch :-)

cheers
maverix
# 5  
Old 02-14-2008
I hope i have understood you correctly: regular expressions can be made to apply only on a limited group of lines:

Code:
sed -n '/start/,/finish/ {
            p
        }

will start printing lines (the "p" command) on the line containing "start" and continue to print the lines until it finds a line containing "finish", when it will stop printing them, until it again encounters a line containing "start", etc.

To print only the last group of lines is a bit tricky: copy everything in one such group to the holdspace, overwriting it every time a new group starts. Upon reaching the last line output the hold space and you are done.

Code:
sed -n '/^Finished/ {
               H
        }
        $ {
               x
               p
        }
        /^Started/,/^Finished/ {
               /^Started/ {
                     h
               }
               /^Started/ !{
                     H
               }
        }'

I hope this helps.

bakunin
# 6  
Old 02-14-2008
Quote:
Originally Posted by maverix
good that you found it useful!!!

check out the man pages for tail, awk and grep to understand how it works.
In particular, check the switches I've used for each command.

Don't think I am being rude to you.
You must have heard the good old story where the fisherman taught a starving person to fish, rather than giving him few of his catch :-)

cheers
maverix
I understood your usage of command.
# 7  
Old 02-14-2008
Quote:
Originally Posted by bakunin
I hope i have understood you correctly: regular expressions can be made to apply only on a limited group of lines:

Code:
sed -n '/start/,/finish/ {
            p
        }

will start printing lines (the "p" command) on the line containing "start" and continue to print the lines until it finds a line containing "finish", when it will stop printing them, until it again encounters a line containing "start", etc.

To print only the last group of lines is a bit tricky: copy everything in one such group to the holdspace, overwriting it every time a new group starts. Upon reaching the last line output the hold space and you are done.

Code:
sed -n '/^Finished/ {
               H
        }
        $ {
               x
               p
        }
        /^Started/,/^Finished/ {
               /^Started/ {
                     h
               }
               /^Started/ !{
                     H
               }
        }'

I hope this helps.

bakunin
How do i use this script, where to put my logfile name Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

3. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

4. Shell Programming and Scripting

How to output all lines following Nth occurrence of string

Greetings experts. Searched the forums (perhaps not hard enough?) - Am searching for a method to capture all output from a log file following the nth occurrence of a known string. Background: Using bash, I want to monitor my Oracle DB alert log file. The script will count the total # of... (2 Replies)
Discussion started by: cjtravis
2 Replies

5. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

6. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

7. Shell Programming and Scripting

Extracting text between two patterns 1 and 2 and pattern2 should be second occurrence of the file

Hi All, I have a small query. I have a file containing the following lines File 1: 29-Jul-2011 GMT Static data requires update <Extraction should start here> ----------- ----------- -------------------- ----------------------- ----------- <should stop here> Pattern1 will be time... (2 Replies)
Discussion started by: gangii87
2 Replies

8. Shell Programming and Scripting

How can I get the lines between two patterns?

hi, I have the following file hello world this is to say bye to everyone so bye I want to get the lines from hello to the first bye inclusive into another file? how can I do this (11 Replies)
Discussion started by: JamesByars
11 Replies

9. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

10. Shell Programming and Scripting

How to get lines in between Patterns?

Hi, I need to create a script that does the following: 1. Read the file for the occurrences of "EXECUTE" and "END" strings. There will be several occurrences of EXECUTE and END strings on the file. 2. The resulting lines in #1, needs to be searched for the word... (11 Replies)
Discussion started by: racbern
11 Replies
Login or Register to Ask a Question