Operating System - HP-UX
1829949 Members
2950 Online
109998 Solutions
New Discussion

command to get the phrase and next line

 
MArk Brook_1
Occasional Contributor

command to get the phrase and next line

Hi,

I need to pull out some information from a log file.

I have a key word which is "Start of" however I also need the line afterwards, this is the date.

I also need to pull out another key word "Done" and the previous line this time.

Example of the file

ie

Start of Log file
11:00 08/11/06
Running copy
Deleting
11:15 08/11/06
Done

The output I want is
Start of Log file
11:00 08/11/06
11:15 08/11/06
Done

I've can only manage to grep out Start Of and Done.

can someone help.

Thanks

Mark
9 REPLIES 9
Pete Randall
Outstanding Contributor

Re: command to get the phrase and next line

Mark,

"Handy One-Liners for Sed" contains an example that prints the line before and after a regular expression:

# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (similar to "grep -A1 -B1")
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h

I'm attaching the rest of the document for future reference.


Pete

Pete
Frank de Vries
Respected Contributor

Re: command to get the phrase and next line

You can do this to get multiple greps.


$>grep -e 'Start' -e ":" -e 'Done' grep.test

Start of Log file
11:00 08/11/06
11:15 08/11/06
Done

or the same result:

$>grep -e 'Start' -e "/" -e 'Done' grep.test

Start of Log file
11:00 08/11/06
11:15 08/11/06
Done





Look before you leap
James R. Ferguson
Acclaimed Contributor

Re: command to get the phrase and next line

Hi Mark:

# perl -ne 'if (/^Start/) {print;$x=1;next};if (/^Done/) {print $hold;print};$hold=$_;if ($x==1) {print;$x=0}' file

Regards!

...JRF...
Victor Fridyev
Honored Contributor

Re: command to get the phrase and next line

Hi,

AWK is not worse:

awk '/Start of Log file/ {getline;print}
/Done/ {print PrevLine}
{ PrevLine=$0 }' logfile

HTH
Entities are not to be multiplied beyond necessity - RTFM
Sandman!
Honored Contributor

Re: command to get the phrase and next line

Yet another way to do the same thing with ex(1):

# ex -s +"/^Start/,/^Start/+1 p | /^Done/-1,/^Done/ p | q!" file

~cheers
Hein van den Heuvel
Honored Contributor

Re: command to get the phrase and next line

Minor correction to Victor's awk suggestio:

awk "/^Start/{print; getline; print} /^Done/ { print old; print} {old=$0}" tmp.txt

This will also print the Start and Done lines as requested.

Hein.
MArk Brook_1
Occasional Contributor

Re: command to get the phrase and next line

Thanks for the help guys. Decided to go with the awk command. Works a treat.

Mark
Frank de Vries
Respected Contributor

Re: command to get the phrase and next line

Please do not forget to assign points:

http://forums1.itrc.hp.com/service/forums/helptips.do?#28
Look before you leap
Dennis Handly
Acclaimed Contributor

Re: command to get the phrase and next line

Correction to Hein's correction of Victor's awk suggestion:

The awk program should typically be in single quotes, not double quotes. In particular the $0 will have problems.