Operating System - Linux
1751765 Members
4609 Online
108781 Solutions
New Discussion юеВ

Re: Find a sentence, skip 5 lines, read lines 6-8

 
amykoh
Super Advisor

Find a sentence, skip 5 lines, read lines 6-8

Hi all,

I have a scenario where a shell script is required to grep and match phrase from a logfile, then skip the next few lines then grep the next subsequent lines.

And I have to display the first grep phrase plus the next subsequent lines.

For example: I want to grep if "Severity............: INFORMATION" exist, then grep from "Summary..." onwards (which is in 2-4 lines depending on the details in Summary.

Results hoped for:
"Serverity............: INFORMATION. Summary: Adapter at hardware path 0/4/1/0/4/0 : CISS: RAID SA controller is now online."

Some challenges:
1. The logfile gets appended- How can the script tell when it read the logfile last?

Event data from monitor:

Event Time..........: Thu Mar 30 11:29:50 2006
Severity............: INFORMATION
Monitor.............: dm_raid_adapter
Event #.............: 2
System..............: SERVER_NAME

Summary:
Adapter at hardware path 0/4/1/0/4/0 : CISS: RAID SA controller is now
online.

Hope some of you gurus can help me.

Thanks.
Amy
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: Find a sentence, skip 5 lines, read lines 6-8

>grep and match phrase from a logfile, then skip the next few lines then grep the next subsequent lines.

What happens if there is more than one occurrence? Did you want to do it only once? Or all occurrences?

You could try:
awk -v START=$number '
/Severity............: INFORMATION/ {
if (NR <= START) next # already done
save_NR = NR
save = $0
getline; getline; getline; getline; getline
save2 = $0
getline
save3 = $0
getline
save4 = $0
print save, save2, save3, save4
}
END { print "Restart after line, save_NR" }
' file

Issues: Not sure how many lines in Summary to print?
You have to take the output of the "Restart after" to feed it back as $number.
Dennis Handly
Acclaimed Contributor

Re: Find a sentence, skip 5 lines, read lines 6-8

Oops, that list line should be:
END { print "Restart after line", save_NR }
amykoh
Super Advisor

Re: Find a sentence, skip 5 lines, read lines 6-8

Thanks for the suggestion.

I have managed to write another script. Thanks
Rodney Hills
Honored Contributor

Re: Find a sentence, skip 5 lines, read lines 6-8

Dennis spent some time coming up with a solution. Sending a couple points his way is a nice way to say thank-you...

my 2 cents

Rod Hills
There be dragons...
amykoh
Super Advisor

Re: Find a sentence, skip 5 lines, read lines 6-8

Thanks for you suggestions!..:)
Dennis Handly
Acclaimed Contributor

Re: Find a sentence, skip 5 lines, read lines 6-8

What Rod was suggesting was that you assign points as a way of a thank you. See the following link:
http://forums.itrc.hp.com/service/forums/helptips.do?#33