Efficient rewrite of code?

egrep -v "#" ${SERVERS} | while read shosts
                        do
                                grep -Pi "[a-z]|[A-Z]" ${LOGFILE} | egrep "${snhosts}" | egrep "NOTIFICATION:" | awk -F";" '{print $3}' | sort -n | uniq | while read CEXIST
                                do
                                        if [ "$CEXIST" = "$rcheckname" ] ; then
                                                echo "$CEXIST"
                                        fi
                                done
                        done

is there a better way to rewrite the above code? im guessing there's some awk magic out there that could work here?

You need show us some samples on log file:

LOGFILE

LOGFILE is just a file with a list of hostnames, such as

skysmart-01.net
skysmart-02.net
#skysmart-03.net
skysmart-04.net
skysmart-05.net
skysmart-06.net

Presumably you mean that is content of the $SERVERS file? What is in the log files and what is in variable $rcheckname ?

correct. my fault.

the log file contains hundreds of lines similar to:

[1356910980] SERVICE NOTIFICATION: AppEngAlerts;skysmart-01.net;PORT_5000_CHECK;CRITICAL;notify-by-email;CRITICAL 

rcheckname is the name of a check. in this case, it would be PORT_5000_CHECK .

so, the if statement verifies the checkname found matches the one on record.

Not sure what you want to achieve. Inferring from pieces of info spread over the posts here and there, you want to check a log file against a list of servers to see if a certain check has been performed, and then output the check's name? You can do that like this:

$ grep -f servers $LOGFILE| grep "NOTIFICATION:.*$rcheckname" > /dev/null && echo $rcheckname 
PORT_5000_CHECK

Depending on the grep version the commented out server line might not be ignored in the search. What be the contents of ${snhosts} ?

Or in bash or ksh93:

grep -f <(sed "/#/d; s/.*/NOTIFICATION.*;&;$rcheckname/" "$SERVERS") "$LOGFILE" |
cut -d\; -f2,3 | sort -u | cut -d\; -f2

awk:

awk '
NR==FNR && !/#/{
  S[$1]
  next
} 
$2 in S && $0~pat && !O[$2]++{
  print $3
}
' pat="NOTIFICATION.*$rcheckname" "$SERVERS" FS=\; "$LOGFILE"
1 Like