AWK/GREP combination question

TwentyFourSeven
TwentyFourSeven used Ask the Experts™
on
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.  
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2013
Top Expert 2013

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

Author

Commented:
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.
Most Valuable Expert 2013
Top Expert 2013
Commented:

command | awk '$3!~"Connected"  {print}'  | while read line
  do
    echo $line | mailx -s 'Lines other than "Connected" found!' recipient@domain.tld  
  done
The above will do "nothing" (not send out mail) if no matching lines are found.
wmp

Author

Commented:
Thank you !
Most Valuable Expert 2013
Top Expert 2013

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

Author

Commented:
Well spotted..... I can see how your original might have been a bit noisy !

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial