Operating System - HP-UX
1846744 Members
5249 Online
110256 Solutions
New Discussion

Re: Please help.. a clever script needed to...

 
Donnie Darko_1
Frequent Advisor

Please help.. a clever script needed to...

Hi All... please help!

I have an ASCII file containing hundreds of ITO/OVO Errors, in a format as follows:-

SEVERITY Critical
APPLICATION "MPG"
MSGGRP "OS"
OBJECT "peanut"
TEXT "Big problems with this jobby"

SEVERITY Warning
APPLICATION "MPG"
MSGGRP "OS"
OBJECT "peanut"
TEXT "Warning, some problems in jobby"

SEVERITY Critical
APPLICATION "MPG"
MSGGRP "OS"
OBJECT "peanut"
TEXT "Mad problems with this jobby"

SEVERITY Normal
APPLICATION "MPG"
MSGGRP "OS"
OBJECT "peanut"
TEXT "This job is OK!"

SEVERITY Critical
APPLICATION "MPG"
MSGGRP "OS"
OBJECT "peanut"
TEXT "I'd check this one out"

...and I need to extract all the Critical instances to a seprate ASCII file. For example, I need to capture every instance of "SEVERITY Critical", and the four lines that follow this statement, and place them in a seperate file, and continue the search in the original file for the next instance, so at the end, I will have a file which reads:-

SEVERITY Critical
APPLICATION "MPG"
MSGGRP "OS"
OBJECT "peanut"
TEXT "Big problems with this jobby"

SEVERITY Critical
APPLICATION "MPG"
MSGGRP "OS"
OBJECT "peanut"
TEXT "Mad problems with this jobby"

SEVERITY Critical
APPLICATION "MPG"
MSGGRP "OS"
OBJECT "peanut"
TEXT "I'd check this one out"

...now I beleive a while read statement will acheive this for me, but my Unix scripting not being what it should be.. I turn to you guys for assistance on this. Can anyone provide a script to do this.. or is it actually possible?? (I am sure it must be!)

Many thanks in advace.....

11 REPLIES 11
Elmar P. Kolkman
Honored Contributor

Re: Please help.. a clever script needed to...

Of course there are a lot of solutions possible. My solution would be something with AWK...

cat | awk '
/SEVERITY/ && /Critical/ { crit=1 }
crit==1 { print }
/^$/ { crit=0 }'

Every problem has at least one solution. Only some solutions are harder to find.
Ralph Grothe
Honored Contributor

Re: Please help.. a clever script needed to...

perl -ne 'print if /Critical/../^\s+$/' /path/to/error.log
Madness, thy name is system administration

Re: Please help.. a clever script needed to...

example
awk '/SEVERITY Critical/,/^\n/{print $0}' file.txt
RAC_1
Honored Contributor

Re: Please help.. a clever script needed to...

Get GNU grep fgrep. This grep can grep a pattern and show x lines after grep pattern.

You can get GNU grep at HP porting centre.
This came up few times in the week. Search the forums.

Anil
There is no substitute to HARDWORK
Elmar P. Kolkman
Honored Contributor

Re: Please help.. a clever script needed to...

Or, using sed:
cat | sed -n 's/SEVERITY Critical/,/^$/p'
Every problem has at least one solution. Only some solutions are harder to find.
RAC_1
Honored Contributor

Re: Please help.. a clever script needed to...

sed -n '/SEVERITY Critical/,/^$/p' < your_file

Anil
There is no substitute to HARDWORK
Elmar P. Kolkman
Honored Contributor

Re: Please help.. a clever script needed to...

Sorry, RAC is right. I had a 's' too much in the sed command when re-typing the command (ITRC is not open on my X-terminal, so I can not copy/paste commands...)
Every problem has at least one solution. Only some solutions are harder to find.
Jean-Luc Oudart
Honored Contributor

Re: Please help.. a clever script needed to...

another awk solution :
awk '{
if(substr($0,1,17)=="SEVERITY Critical") {
for(i=0; i < 5;i++) {print $0; getline;}
}
}'

Regards,
Jean-Luc
fiat lux

Re: Please help.. a clever script needed to...

my mistake

not
awk '/SEVERITY Critical/,/^\n/{print $0}' file.txt

only
awk '/SEVERITY Critical/,/^$/{print $0}' file.txt
Donnie Darko_1
Frequent Advisor

Re: Please help.. a clever script needed to...

Thanks guys... some great tips there...

However.. the file is not as clean as I have made it appear above. It also has a lot of unwanted Junk in there, but I am only interested in the messages which appear above out of it. They are all indented in the file, eg:-

blah, blah, blah, blah........

SEVERITY Critical (Needed)
Line 1 (Needed)
Line 2 (Needed)
Line 3 (Needed)
Line 4 (Needed)

blah, blah, blah, blah,
blah, blah, blah, blah

.. u get this idea.. so I am sorry for not clarifying this initially! If anyone wants me to send them the file (4mb) or attach it here (Can I do that with 4mb!?) I will do so...

Many thanks again


Jeroen Peereboom
Honored Contributor

Re: Please help.. a clever script needed to...

Let's assume your file contains blocks of messages and the first line determines if the block must be printed or not.

Take Elmar's solution, and modify it:
awk '
/SEVERITY/ && /Critical/ { crit=1 }
/SEVERITY/ && !/Critical/ { crit=0 }
crit==1 { print }
'