Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Reading from file or variable and extract in between information

 
Highlighted
Super Advisor

Reading from file or variable and extract in between information

Hi all,

I need help to in extraction. I have a sed command which filters particular lines I want.

This is then stored into a variable. Or if can be pumped into another file, depending on how best the script is written.

If pumped into a file, below is the output:

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

If in a variable, it will be in one line.

Basically, what I need to do is read the file/variable and extract and trigger an opcmsg command if it matches "Severity......: Critical" and printing the relevant Summary information with it.

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

Hope you can help me.

Thanks.
Amy
9 REPLIES 9
Highlighted
Acclaimed Contributor

Re: Reading from file or variable and extract in between information

If you have a file with Severity, Summary and one additional line, then you can do:
TMP=/var/tmp/ae.$$
awk '
/^Severity.*Critical/ {
print $0
getline
print $0
getline
print $0
} ' file > $TMP.2

if [ -s $TMP.2 ]; then
    echo opcmsg
    cat $TMP.2
fi
rm -f $TMP.2

Highlighted
Super Advisor

Re: Reading from file or variable and extract in between information

Hi Dennis,

Please forgive my noobness, but what does this line mean?

TMP=/var/tmp/ae.$$

is it a path? or part of the awk statement? does the path depend on the path of my file?
Highlighted
Acclaimed Contributor

Re: Reading from file or variable and extract in between information

>but what does this line mean?
TMP=/var/tmp/ae.$$
>is it a path?

It is a fairly unique temporary file, based on the PID, to keep the extracted info.
Highlighted
Super Advisor

Re: Reading from file or variable and extract in between information

I see

I ran your script. The cat $TMP.2 output shows 3 entries of
"Severity............: INFORMATION
Summary:
Adapter at hardware path 0/4/1/0/4/0 : CISS: RAID SA controller is now
online."

How can I make it to read each entry as one opcmsg?

Seeing from the cat output, looks like it will trigger one opcmsg with 3 entries.

amy
Highlighted
Acclaimed Contributor

Re: Reading from file or variable and extract in between information

>How can I make it to read each entry as one opcmsg? Seeing from the cat output, looks like it will trigger one opcmsg with 3 entries.

No clue what a "opcmsg" is. What does it take? A string or a file or what?

If you want to jam them together you can do:
$(< $TMP.2 )
Highlighted
Super Advisor

Re: Reading from file or variable and extract in between information

Hi Dennis,

opcmsg is a command to trigger an event alert in Openview Operations for Windows (OVOW) console. OVOW is a fault and performance monitoring application.

I would like the script to read the file (till EOF), detect EACH occurance of the specified pattern ("Severity............: INFORMATION
Summary:
Adapter at hardware path 0/4/1/0/4/0 : CISS: RAID SA controller is now
online."), and trigger the opcmsg command.

hth explains more..:)

Amy
Highlighted
Acclaimed Contributor

Re: Reading from file or variable and extract in between information

>opcmsg is a command to trigger an event alert

This is useless, you didn't answer:
What does it take? A string or a file or what?

Is it?
opcmsg severity=warning a=a o=o msg_text="test" msg_grp="test"


>EACH occurance of the specified pattern ("Severity............: INFORMATION
Summary:

You originally said, "Critical" and now you said "INFORMATION", which is it?

>trigger the opcmsg command.

So you want to do more than once?
Highlighted
Super Advisor

Re: Reading from file or variable and extract in between information

Yes, those are the parameters for opcmsg.

opcmsg severity=warning a=a o=o msg_text="test" msg_grp="test"

Occurance pattern would be "Critical".

Yes, if there are a few "Critical" occurances, it should trigger a opcmsg command for each time it finds the pattern.
Highlighted
Acclaimed Contributor

Re: Reading from file or variable and extract in between information

Ok, this script has awk calling the system command to invoke opcmsg. I'm not sure what you want to do with severity=, a=, o= or msg_grp=. I've removed the Severity line and "Summary:".

awk '
/^Severity.*Critical/ {
getline # skip "Summary:"
getline
system("echo opcmsg severity=warning a=a o=o msg_text=\""
$0 "\" msg_grp=\"test\"")
} ' file