`tail -f` until text is seen

You can pipe the tail -f into sed, telling it to quit when it sees the line you're searching for:

tail -f /path/to/file.log | sed '/^Finished: SUCCESS$/ q'

sed will output each line it processes by default, and exit after it sees that line. The tail process will stop when it tries to write the next line and sees its output pipe is broken


tail -f my-file.log | grep -qx "Finished: SUCCESS"

-q, meaning quiet, quits as soon as it finds a match

-x makes grep match the whole line

For the second part, try

tail -f my-file.log | grep -m 1 "^Finished: " | grep -q "SUCCESS$"

-m <number> tells grep to stop after number matches

and the grep -q exit status will only be 0 if SUCCESS is found at the end of the line

If you want to see all the output, you can't use grep -q, but you can still do

tail -f my-file.log | grep -m 1 "^Finished: "

which does everything except set the exit status to 1 if FAILURE appears.


A variation on @Mikel's answer with @Mrozek's comments (I would have replied on the comment but I think i don't have enough privileges yet)

tail -f my-file.log | tee >( grep -qx "Finished: SUCCESS" )

would allow you to use @Mikel's solution and still see the output on the screen

Tags:

Tail

Logs