Parsing a $VARIABLE within a script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Parsing a $VARIABLE within a script.
# 1  
Old 09-25-2012
Question Parsing a $VARIABLE within a script.

Hello all,

I have a situation where I need to parse for certain items from a $VARIABLE within a sh script.
  1. The sh script is run when an alert comes in.
  2. The alert data payload has a Message field called "EVENTMSG"
  3. The script that is run takes the "EVENTMSG" and prints it out to the ticket.
  4. I need to parse the "EVENTMSG" for additional information.
The line inside the sh script where this takes place is simply an echo:

echo "EventMessage: $EVENTMSG"

Is there a way to parse the $EVENTMSG by using RegEx? AND can I put the results of the RegEx as another variable WITHIN the same script?

My desired output is something like this:
(within the script)
blah blah blah...
echo "EventMessage: $EVENTMSG"
SEV=".+AlarmSeverity=\s(\S+).+" ((where $SEV is taken somehow from within the $EVENTMSG))
Smilie
MANY THANKS IN ADVANCE!!!
# 2  
Old 09-25-2012
The simple answer is "Yes." You can do all of that. But, to figure out what needs to be done here, we need to know a lot more about the format of the string referenced by $EVENTMSG and we need to know what you want out of $EVENTMSG besides the substring that follows AlarmSeverity=.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-25-2012
$EVENTMSG details

Thanks for your immediate response!!

To answer your question, the $EVENTMSG could look like some 40,000 different messages taken directly from 40,000 Event files.

There is one part in every single file that is the same... the time/day/date stamp, followed by "ModelType={t}, ModelName={m}"... but I may not necessarily need to parse that out.

The {t} and {m} are variables from another program that will input the appropriate data, so when it arrives in the $EVENTMSG it has usable data.

As far as "what I want out of the $EVENTMSG besides the substring that follows (AlarmSeverity=)", the short answer is, it depends on the script that I am using this in. In other words, I have a process in place that will send SPECIFIC alerts depending on a host of alert filters to the process that runs this sh script. The script, itself, is called SetScript.sh, and is called when the alert filters are passed. There is a separate SetScript for each type of alert filter that I create, so that means that what I need out of the $EVENTMSG will depend on the filter that it passes.

Not much of an answer, but that's what I have to work with Smilie

If you need any more details please let me know.

Thanks again Don~!!!
# 4  
Old 09-25-2012
Quote:
Originally Posted by dlundwall
Thanks for your immediate response!!

To answer your question, the $EVENTMSG could look like some 40,000 different messages taken directly from 40,000 Event files.

There is one part in every single file that is the same... the time/day/date stamp, followed by "ModelType={t}, ModelName={m}"... but I may not necessarily need to parse that out.

The {t} and {m} are variables from another program that will input the appropriate data, so when it arrives in the $EVENTMSG it has usable data.

As far as "what I want out of the $EVENTMSG besides the substring that follows (AlarmSeverity=)", the short answer is, it depends on the script that I am using this in. In other words, I have a process in place that will send SPECIFIC alerts depending on a host of alert filters to the process that runs this sh script. The script, itself, is called SetScript.sh, and is called when the alert filters are passed. There is a separate SetScript for each type of alert filter that I create, so that means that what I need out of the $EVENTMSG will depend on the filter that it passes.

Not much of an answer, but that's what I have to work with Smilie

If you need any more details please let me know.

Thanks again Don~!!!
You're right. That doesn't give us much of an answer.

Please show us a few examples (using code tags) of actual event messages.
Then given those messages, tell us exactly what you want to get as a result of processing them. If what you want to get out of a message depends on a SetScript, tell us how to find the SetScript associated with an arbitrary message and show us relevant examples of SetScript contents.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-25-2012
This might get you started:

Code:
EVENTMSG="Wed 26 Sep, 2012
 ModelType= Event message testing
 ModelName= Alpha Test
 AlarmSeverity= Critical
 ActionEvent= Evacuate Immediately
 Payload= large raw data string"
 
SEV=$(echo "$EVENTMSG" | sed -nE 's/.+AlarmSeverity=\s(\S+)/\1/p')
echo $SEV

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 09-26-2012
Example which may help...

Quote:
Originally Posted by Don Cragun
You're right. That doesn't give us much of an answer.

Please show us a few examples (using code tags) of actual event messages.
Then given those messages, tell us exactly what you want to get as a result of processing them. If what you want to get out of a message depends on a SetScript, tell us how to find the SetScript associated with an arbitrary message and show us relevant examples of SetScript contents.
let me see if I can supply what you are asking for:

Here is a sample of an event message file:
Code:
{d "%w- %d %m-, %Y - %T"} - Device {m} of type {t} is no longer responding to requests.  This condition has persisted for more than 3 minutes.
 
Alert Code: {S 1}
Server Instance: {I 2}
Severity: {S 100}

Here is a sample of the same event message when I get it in the application with an alert that has been processed:
Code:
Sept 21, 2012 8:21:55 PM MDT - Device Main_Server_01 of type Host_Server is no longer responding to requests.  This condition has persisted for more than 3 minutes.
 
Alert Code: Danger
Server Instance: 31415
Severity: Critical

I am looking to get, as an example, the Server Instance value. The event file uses {I 2} which represents the 2nd varbind in the trap that is being sent, and the value is an Integer. I obviously don't care about the literal "{I 2}" portion, I am interested in the value that it is bringing back: 31415.

So in the SetScript it would look something like this:

Code:
# echo "EventMessage: $EVENTMSG"
echo "Server Instance Error: " $SI

where $SI is the value taken from the $EVENTMSG that I will hopefully be able to parse.
# 7  
Old 09-27-2012
The solution I posted earlier should work for this data eg:

Code:
EVENTMSG="Sept 21, 2012 8:21:55 PM MDT - Device Main_Server_01 of type Host_Server is no longer responding to requests.  This condition has persisted for more than 3 minutes.
 
 Alert Code: Danger
 Server Instance: 31415
 Severity: Critical"
SI=$(echo "$EVENTMSG" | sed -nE 's/.+Server Instance:\s(\S+)/\1/p')
echo $SI

Output is:
Code:
31415

If you know extended regular expressions, and I suspect you do as you posted the above RE in your first post.
You should be able to tailor the sed command to fetch any value you are after.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable of Path directory is not parsing in awk

Hi All, i had to split one files into 10 equally. For that i have coded below awk. OUTPUT_FILE=/home/sit/path/Files/file_EXPORT.lst DIR_NM=`dirname ${OUTPUT_FILE}` awk -v CURR_DATE="$(date +'%d-%m-%Y-%H-%M')" -v pth=$DIR_NM '{print >> pth/"tgt_file_name"CURR_DATE"_"NR%10 }' ${OUTPUT_FILE} ... (7 Replies)
Discussion started by: looney
7 Replies

2. Shell Programming and Scripting

Parsing Output of a Variable

i have a log file that contains something similar to this: one two three four five six seven eight nine ten eleven twelve thirteen fourteen one two three four five six seven eight nine ten eleven twelve thirteen fourteen one two three four five six seven eight nine ten eleven twelve... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

XML parsing with a variable

I have the following XML <Audit_Type>1</Audit_Type><Session_Id>34505863</Session_Id> <StatementId>1</StatementId><EntryId>1</EntryId> <Extended_Timestamp>2012-03-06T10:25:20.789459</Extended_Timestamp> <DB_User>KASINIY</DB_User> <OS_User>majohn1</OS_User><OS_Process>28636</OS_Process>... (3 Replies)
Discussion started by: BeefStu
3 Replies

4. Shell Programming and Scripting

Parsing file list in variable

Hello, somewhere in a shell script, i am storing the output of "ls" into a variable. My question is how can i parse this variable to get each filepath. I don't want to create a temporary file to write down all the filenames and then parse it.. is there a easy way out.. here is what... (3 Replies)
Discussion started by: prasbala
3 Replies

5. UNIX for Dummies Questions & Answers

Parsing a variable

Can someone help me? I have been looking in the archives as I am sure this is very simple to do, but I do not know. I have a variable which sometimes contains a file name and sometimes contains a fully qualified file name. I want to be able to separate the directory from the file name into 2... (3 Replies)
Discussion started by: CAGIRL
3 Replies

6. Shell Programming and Scripting

Parsing a variable length file

Hi I am new to shell scripting. I need to parse a file which contains the header and detail records and split into n of file based on dept ID, for ex. INPUT FILE: DEPT ID: 1 EMPNAME: XYZ EMPAddress: XYZZZ DEPT ID: 2 EMPNAME: ABC EMPAddress: ABCD DEPT ID: 1 EMPNAME: PQR EMPAddress:... (6 Replies)
Discussion started by: singhald
6 Replies

7. Shell Programming and Scripting

parsing a variable

Hi, I want to get an input from user and parse the input. The legal characters allowed in the input are alnum(a-zA-Z0-0), . , - Also the first and las characters must be alnum only. e.g if the input is abc.ghh-sok.com then the script should return correct, and if the input is like... (2 Replies)
Discussion started by: g_rohit7
2 Replies

8. Shell Programming and Scripting

parsing a string into variable

I know solution to this but I was wondering if its easier than what i think I have to pass 20 parameters to a script, which of course is not working so I parsed $3 to be a pipe deliminated string for instance below a.ksh One Two Compa|Compb|Compc|compd|............. Now i have to read... (5 Replies)
Discussion started by: Anubhav
5 Replies

9. UNIX for Dummies Questions & Answers

Parsing a variable length record

I need to pick a field out of a variable record - the field is always found 4 fields after a certain text string, but it can be on any line of the record and in any position across the record on a line. I have had no luck through any of the Unix editors being able to cut a field that isn't always... (17 Replies)
Discussion started by: Barb
17 Replies

10. Shell Programming and Scripting

Parsing a variable string

Hi all, I have a problem surfacing and I hope you all could help. What I have to do is take a input file and fill out a fax template from that file. The biggest problem I found was I have to parse the string "//FAX(faxnumber=555-5555;style="style1"; and on and on. The string can be in any... (5 Replies)
Discussion started by: pageld
5 Replies
Login or Register to Ask a Question