| | |
Gathering data from specific lines
Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2006
Posts: 17
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Apr 2006
Posts: 148
Reputation:
Solved Threads: 40
Shell Scripting Syntax (Toggle Plain Text)
awk 'BEGIN{FS="[: ]"} /Name/{ name=$3 } /Active/ && /Yes/{ cmd = "mailx -s \""name " is active\" body root" system(cmd) } ' file
•
•
Join Date: Feb 2006
Posts: 17
Reputation:
Solved Threads: 0
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):
And here's my script right now:
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?
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)
------------------------------------------------------------ Policy Name: _suspended Policy Type: Standard Active: no Effective date: 08/13/2007 17:08:18 More lines
Shell Scripting Syntax (Toggle Plain Text)
awk 'BEGIN{FS="[: ]"} /Name/{ name=$1 } /Active/ && /no/{ print name " is NOT active!" } ' /tmp/policy.txt
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?
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: read from file
- Next Thread: 101 scripting questions
| Thread Tools | Search this Thread |





