tail and grep + print and exit first match

Viewed 1503

I'm looking for a 1 liner to tail a file and grep a "string", print the first match (new line) and exit.

I came up with:

tail -f /var/log/logfile.log -n 0 | grep -m 1 -i string_to_match

actual result is that the command prints the first match but exits only after the second match. any help would be appreciated

1 Answers

In Bash you could use:

$ grep -m 1 string_to_match <(tail -n 0 -f file)

This could work for testing (NOTICE: IT APPENDS TO A FILE NAMED file):

$ grep -m 1 foo <(tail -n 0 -f file) & sleep 2 ; echo -e bar\\nfoo >> file
[1] 5390
foo
[1]+  Done                    grep --color -m 1 foo <(tail -n 0 -f file)

Edit: Further testing revealed that tail stays running in the background but exits after the next line in the file.

Related