tep
tep

Reputation: 35

Using awk to find matching content

I am VERY new to awk, and trying to use it to parse out a log file. The file contains information of flash version, if installed. The line I'm looking for is like:

Fri Apr  8 11:38:39 EDT 2016: Current Flash version: noflash

So I wrote a script to search:

#!/bin/bash

NOFLASH=`awk -F ' ' '($2 == "Apr") && ($3 == "8") && ($10 == "noflash") { print $10 }' /Library/Logs/FlashUpdateScript.log`

if [ "$NOFLASH" = *noflash* ];then  
echo "Flash not installed on Apr 8" 
else echo "Flash was installed on Apr 8" 
fi

The problem is that there can be multiple lines that contain Apr 8 and noflash, so in those cases, it's not returning the "Flash not installed" value I'm looking for. How do I edit this script so I can tell if flash wasn't installed on Apr 8?

Upvotes: 2

Views: 67

Answers (2)

Walter A
Walter A

Reputation: 19982

With grepyou can match what you want:

logfile="/Library/Logs/FlashUpdateScript.log"
grep "Fri Apr  8 .*Current Flash version: noflash" ${logfile}

You can use this in a script, something like

if [ -n "$(grep "Fri Apr  8 .*Current Flash version: noflash" ${logfile})" ]; then
   echo "Flash not installed on Apr 8" 
else
   echo "Flash was installed on Apr 8" 
fi

Upvotes: 1

Tom Fenech
Tom Fenech

Reputation: 74596

If all you want to do is print the message, then I'd just do the whole thing in awk:

awk '($2 == "Apr") && ($3 == "8") && ($10 == "noflash") { f = 1 } 
END { print "Flash", (f ? "not" : "was"), "installed on Apr 8" }' /Library/Logs/FlashUpdateScript.log`

I removed the custom field separator as the default works fine for your case. A variable f is set based on the same conditions you were using. Once the file has been processed, the message is printed, inserting the word "not" or "was" depending on whether f is true.

Upvotes: 0

Related Questions