Luca Luca - 11 months ago 44
Linux Question

BASH Linux exit read log until specific string

I must read the log until I receive “

Server Startup

When I get the string “
”, I must exit the
command and proceed through the rest of the script operations.

Now the syntax is:

cd /opt/path/path/path
LOG_FILE=$(ls -ltr | tail -1 |awk '{print $9}')
tail -f $LOG_FILE

Thanks in advance

Answer Source
stdbuf -oL tail -f log.file | stdbuf -iL awk '/startup/ {print "Match found";exit}'

This will check for string startup and when it is found, it will print "Match found" and then exit from log monitoring. stdbuf to provide line buffered to tail and awk.