0

i'd like to be able to do multiple search on the same file and if found on any pattern, perform an action. Currently its only searching for "ERROR" and the idea is to search for "PANIC:" etc. Please advise. thanks

newlog=PG_messages_${day}.new
oldlog=PG_messages_${day}.old
log=postgresql-${day}.log

touch ${newlog} ${oldlog}
mv ${newlog} ${oldlog}

grep "ERROR:" $log > $newlog 2>&1
diff  $newlog $oldlog 1>/dev/null 2>&1

    if [ $? -ne 0 ]; then
        diff  $newlog $oldlog >> /tmp/$$
       cat /tmp/$$ 
    fi
}
nick
  • 99
  • 7
  • Look into the `-e`/`--regexp` option: `echo $'foo\nbar' | grep -e foo -e bar` – Ionuț G. Stan Jun 21 '22 at 11:26
  • 1
    as in ``` grep -e "ERROR:" -e "PANIC:" -e "WARNING" $log ``` ? – nick Jun 21 '22 at 11:28
  • you can provide multiple `-e` and/or `-f` arguments – jhnc Jun 21 '22 at 11:29
  • Tangentially, [Why is testing “$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Jun 21 '22 at 11:33

1 Answers1

0
grep "(ERROR|PANIC):" $log > $newlog 2>&1
RET_GREP=$?
if [ $RET_GREP -eq 0 ]; then
    # do something when something found
    :
elif [ $RET_GREP -eq 1 ]; then
    # do something when nothing found
    :
else
    # on error...
    :
fi
Arnaud Valmary
  • 2,039
  • 9
  • 14
  • Hi, Arnaud, the above does not work as it greps nothing resulting in no diff... Here is the output in debug mode .... `+ grep '(ERROR|PANIC|FATAL):' postgresql-Tue.log + diff PG_messages_Tue.new PG_messages_Tue.old + '[' 0 -ne 0 ']' ` – nick Jun 21 '22 at 12:40