The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to get lines in between Patterns? racbern Shell Programming and Scripting 11 04-23-2008 04:28 AM
how to grep for lines between 2 give patterns? melanie_pfefer Shell Programming and Scripting 1 04-03-2008 08:38 AM
Replace second occurrence only lyoncc Shell Programming and Scripting 5 12-26-2007 07:21 PM
awk + last occurrence agibbs UNIX for Dummies Questions & Answers 2 10-06-2007 12:32 PM
Grep for the same occurrence or maybe Sed hcclnoodles Shell Programming and Scripting 4 09-11-2002 09:25 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-14-2008
rvz rvz is offline
Registered User
 

Join Date: Feb 2008
Posts: 5
Stumble this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-14-2008
Registered User
 

Join Date: Jun 2007
Posts: 12
Stumble this Post!
Try this

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

cheers
maverix
Reply With Quote
  #3 (permalink)  
Old 02-14-2008
rvz rvz is offline
Registered User
 

Join Date: Feb 2008
Posts: 5
Stumble this Post!
thank you very much
it is working fine
Can you explain your script for my understanding
Reply With Quote
  #4 (permalink)  
Old 02-14-2008
Registered User
 

Join Date: Jun 2007
Posts: 12
Stumble this Post!
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
Reply With Quote
  #5 (permalink)  
Old 02-14-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,099
Stumble this Post!
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
Reply With Quote
  #6 (permalink)  
Old 02-14-2008
rvz rvz is offline
Registered User
 

Join Date: Feb 2008
Posts: 5
Stumble this Post!
Quote:
Originally Posted by maverix View Post
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.
Reply With Quote
  #7 (permalink)  
Old 02-14-2008
rvz rvz is offline
Registered User
 

Join Date: Feb 2008
Posts: 5
Stumble this Post!
Quote:
Originally Posted by bakunin View Post
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0