2

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?

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
tep
  • 35
  • 5
  • 1
    Your problem is in the shell script, not the `awk`. You probably want `if [[ "$NOFLASH" == *noflash* ]]` to get matching. As it stands, unless you have a file containing `noflash` in the name, you are looking for `*noflash*` literally, not with globbing. You should be able to [debug your Bash script](https://stackoverflow.com/questions/951336/how-to-debug-a-bash-script/951352#951352) with `bash -x` and see what is going on. I'm not clear what you want report if there was no Flash, then Flash was installed, then uninstalled, etc. – Jonathan Leffler Apr 10 '16 at 16:59
  • 1
    wouldn't it be better to use grep to do the job? – W.F. Apr 10 '16 at 17:27
  • Please edit your question to include a [mcve]. In the sample input/output include lines that should and should not match. Why are you using shell to test the output of awk before printing a message instead of just printing the message from awk? – Ed Morton Apr 10 '16 at 23:20

2 Answers2

1

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
Walter A
  • 19,067
  • 2
  • 23
  • 43
0

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.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141