943,832 Members | Top Members by Rank

Ad:
Jun 23rd, 2008
0

Gathering data from specific lines

Expand Post »
I'd like to create a script that will run a command, parse the output for certain fields/words and then email that information to me if it meets simple criteria.

For example, when run, the command will output several lines. I'll need to grab the


Name: Test1
Type: Windows
Active: No
blah: blah
blah2: blah blah blah

Name: Test2
Type: Unix
Active: Yes
blah: blah
blah2: blah blah blah

Given the above, I want to check to see if it is Active, and if so, email me the names of all that match that criteria? What I am struggling with is telling the script to look at the first X lines, look for the one that says "Active", test for "No" and if so, email me the Name field.

Hope that is not confusing and appreciate the help.

Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rusman is offline Offline
17 posts
since Feb 2006
Jun 23rd, 2008
0

Re: Gathering data from specific lines

Hey There,

You can use grep (case sensitive by default) to find "Active: Yes", check the value of $? (errno) to determine if the match was successful (0 should indicate a successful match) and then just cat the file and pipe it to sendmail or use the file as input to a command like mailx, etc.

What have you got so far, script-wise?

Best wishes,

Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Jun 24th, 2008
0

Re: Gathering data from specific lines

Shell Scripting Syntax (Toggle Plain Text)
  1. awk 'BEGIN{FS="[: ]"}
  2. /Name/{ name=$3 }
  3. /Active/ && /Yes/{
  4. cmd = "mailx -s \""name " is active\" body root"
  5. system(cmd)
  6. }
  7. ' file
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Jun 25th, 2008
0

Re: Gathering data from specific lines

Ghostdog,

Thanks for the great code example! I am mostly there. Here's what my sample data actually looks like (yes, each Policy entry will have the ---- as a separator):
Shell Scripting Syntax (Toggle Plain Text)
  1. ------------------------------------------------------------
  2.  
  3. Policy Name: _suspended
  4.  
  5. Policy Type: Standard
  6. Active: no
  7. Effective date: 08/13/2007 17:08:18
  8. More lines
And here's my script right now:
Shell Scripting Syntax (Toggle Plain Text)
  1. awk 'BEGIN{FS="[: ]"}
  2. /Name/{ name=$1 }
  3. /Active/ && /no/{
  4. print name " is NOT active!" }
  5. ' /tmp/policy.txt
I had to change the name variable to $1 otherwise it wouldn't show anything. The script is working, but the output is not just the deactivated policy name, it's the whole line. From the example above it would output:
Policy Name: _suspended is NOT active!

I can't figure out how to get the "Policy Name:" and all the whitespace removed from the output. I thought that was what the separator did? I tried changing that to \t, [: ], but that still didn't do anything. Ideas?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rusman is offline Offline
17 posts
since Feb 2006
Jun 25th, 2008
0

Re: Gathering data from specific lines

Hey there,

For a line like

Policy Name: _suspended

with : as the separator,

"Policy Name:" would be $1
the rest of the line would be $2.

Can you post one full record, your script (if you've changed it since) and the output from your output text (limit to the one record if you want to keep it short or if there is a privacy issue involved).

Best wishes,

Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Jun 26th, 2008
0

Re: Gathering data from specific lines

I took out the brackets so that it was just FS":" and that worked. I'm not sure what the brackets do.

I'm working on the rest of it now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rusman is offline Offline
17 posts
since Feb 2006
Jun 27th, 2008
0

Re: Gathering data from specific lines

Hey There,

If I'm not mistaken, which I may be, brackets in awk indicate a range or collection of possible choices.

For instance [AbC]

would match

A
b
or
C

Further elaboration is probably available.

Hope that helps.

Best of luck,

Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: read from file
Next Thread in Shell Scripting Forum Timeline: 101 scripting questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC