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

Try this

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

cheers
maverix

thank you very much
it is working fine
Can you explain your script for my understanding

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 :slight_smile:

cheers
maverix

I hope i have understood you correctly: regular expressions can be made to apply only on a limited group of lines:

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.

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

I hope this helps.

bakunin

I understood your usage of command.

How do i use this script, where to put my logfile name :confused:

man sed

either this way: "sed -n '<program>' <filename>"

or that way: "cat <filename> | sed -n '<program>'"

bakunin

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

can you please tell in detail how is this working, i know there is only some minore changes in the above code to get the first occurrence of Started and Finished. But didn't understand the above code properly
Please help.