I am running a process in linux which logs the status and others info in a log file /home/path/Detail_File.log in text format. the file is being continuously written using a background process. I can display the file contents using tail -f /home/path/Detail_File.log.
I want to check the file for a key word keyword (say). How do I continuously check if the key word is found, and if found get that line in a variable.
I am not sure what should I search on internet so posting this question directly without the primary research. Any suggestion would be appreciated.
EDIT 1: Adding screenshot of the while loop suggestion by @jhnc

The echo "Time elapsed: $curr_time ..." is executed and then its silent
CodePudding user response:
If you are using GNU utilities, grep has a --line-buffered option:
tail -n 1 -f /home/path/Detail_File.log |\
grep --line-buffered -F keyword |\
while read matchedline; do
dosomething "$matchedline"
done
This is a pipeline: cmd1 | cmd1 | cmd3
Backslash (\) at end of line allows using linebreak for readability.
tailoutput is fed intogrep.grepoutput is fed into thewhileloop.readgets its input fromgrep, not from the user.
Usually, grep output is buffered - the output only becomes available once the pipe buffer is full. The --line-buffered option causes the pipe buffer to be flushed after every line. For example, consider difference between output of:
while sleep 1; do date; done |\
grep --line-buffered . |\
while read line; do echo "$(date) :: $line"; done |\
head
versus (may take a few minutes before you see anything):
while sleep 1; do date; done |\
grep . |\
while read line; do echo "$(date) :: $line"; done |\
head
