Link to home
Start Free TrialLog in
Avatar of TwentyFourSeven
TwentyFourSeven

asked on

AWK/GREP combination question

Hello,

Assuming I've got an output similar to the following to parse :

One      12345      Connected
Two      67890      Connected

Open in new window


My questions are as folllows :

(1) I would like to display only rows where column 3 status has changed.  "grep -v" is too coarse, it looks at entire rows,  and "awk {print $3} | grep -v" doesn't do the job of outputting whole rows.    Unfortunatley my shell coding expertise is limited to know of other options !

(2) What is an efficient way of continuing the script if there is output from the above ?

count =  `  some piped commands | wc -l `
if [$count -gt 0]
then
 do something
fi

Open in new window


The above does not seem very efficient, because I would need to call the piped command again in order to be able to use its output.  
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Hi,
I'm not quite sure what you're trying to accomplish.
Just checking the third column for not containing "Connected" and outputting those lines is quite simple -
command | awk '$3!~"Connected"  {print}'
This will examine every line command produces.
To process the displayed data you could do

command | awk '$3!~"Connected"  {print}'  | while read line
  do
    ## do something with $line, maybe split into variables $1, $2, $3
   set $line
    echo "1st word is " $1
    echo "2nd word is " $2
    echo "3rdword is " $2
done  
What else would you like to do?
wmp
Avatar of TwentyFourSeven
TwentyFourSeven

ASKER

woolmilkporc,

Thanks for your quick reply, sorry I was not very clear.

Basically in the long run, I want to generate an alert email showing which lines are not "Connected".

If everything is "Connected", no email should be sent.

Hope this clarifies.
ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you !
Thank you for the points, but I think the script will not do exactly what you're expecting, because it will send out one mail per matching line, which is a bit silly!
Let's do it this way
MATCH=$(command | awk '$3!~"Connected"  {print}')
[[ ! -z "$MATCH" ]] && echo "$MATCH" | mailx -s 'Lines other than "Connected" found!' recipient@domain.tld
One run, one mail, regardles of the number of lines!
wmp
Well spotted..... I can see how your original might have been a bit noisy !