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!

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

awk 'BEGIN{FS="[: ]"}
/Name/{ name=$3 }
/Active/ && /Yes/{
  cmd = "mailx -s \""name " is active\" body root"  
  system(cmd)
}
' file

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):

------------------------------------------------------------

Policy Name:       _suspended

  Policy Type:         Standard
  Active:              no
  Effective date:      08/13/2007 17:08:18
  More lines

And here's my script right now:

awk 'BEGIN{FS="[: ]"}
/Name/{ name=$1 }
/Active/ && /no/{
        print name " is NOT active!" }
' /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?

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.