The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 07:28 AM
how to grep for lines between 2 give patterns? melanie_pfefer Shell Programming and Scripting 1 04-03-2008 11:38 AM
Replace second occurrence only lyoncc Shell Programming and Scripting 5 12-26-2007 11:21 PM
awk + last occurrence agibbs UNIX for Dummies Questions & Answers 2 10-06-2007 03:32 PM
Grep for the same occurrence or maybe Sed hcclnoodles Shell Programming and Scripting 4 09-12-2002 12:25 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-14-2008
rvz rvz is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 5
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 (permalink)  
Old 02-14-2008
maverix maverix is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 12
Try this

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

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

Join Date: Feb 2008
Posts: 5
thank you very much
it is working fine
Can you explain your script for my understanding
  #4 (permalink)  
Old 02-14-2008
maverix maverix is offline
Registered User
  
 

Join Date: Jun 2007
Posts: 12
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 (permalink)  
Old 02-14-2008
rvz rvz is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 5
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.
  #6 (permalink)  
Old 02-14-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,628
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
  #7 (permalink)  
Old 02-14-2008
rvz rvz is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 5
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
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:41 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0