Gathering data from specific lines

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2006
Posts: 17
Reputation: rusman is an unknown quantity at this point 
Solved Threads: 0
rusman rusman is offline Offline
Newbie Poster

Gathering data from specific lines

 
0
  #1
Jun 23rd, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Gathering data from specific lines

 
0
  #2
Jun 23rd, 2008
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Gathering data from specific lines

 
0
  #3
Jun 24th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: rusman is an unknown quantity at this point 
Solved Threads: 0
rusman rusman is offline Offline
Newbie Poster

Re: Gathering data from specific lines

 
0
  #4
Jun 25th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Gathering data from specific lines

 
0
  #5
Jun 25th, 2008
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 17
Reputation: rusman is an unknown quantity at this point 
Solved Threads: 0
rusman rusman is offline Offline
Newbie Poster

Re: Gathering data from specific lines

 
0
  #6
Jun 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Gathering data from specific lines

 
0
  #7
Jun 27th, 2008
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC