Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

There is /location/of/thefile, which is a continuously changing logfile. The average density of refreshes is 4 per minute, the possible maximal refresh rate could be 30-40 per minute. Every refresh adds 2-5 lines (average), but it could be hundreds in extreme cases. Every line begins with a [YYYY-MM-DD HH:MM:SS] timestamp followed by plaintext (100-200, max. a few hundred characters).

My task is to construct a simple command which continuously watches this logfile, and sends to the stdout every lines that contain the foo OR bar alphabetical strings. Before and after those (sub)strings there could be any characters (\n only after the (sub)string, of course), even \0. The capitalization of the words could be all of the possible variations.

Well, my ideas for the solution always contain syscalls for the timing, but I shouldn't use them. Please construct me a simple command. Thanks a very lot!

share|improve this question
This is often called “tailing”, from tail -f. See tail for other related questions, where you'll find fancier programs that can filter and color log lines. –  Gilles Apr 7 '11 at 21:17
add comment

2 Answers

I might be misunderstanding the question, but is there a reason you can't use this?

tail -f /location/of/thefile | grep -i -E "foo|bar"

share|improve this answer
Does piping the output of tail -f to grep really work like that? If so, I'm going to have to start using that myself! For a case like this I would have suggested a watch command, but if this does indeed work it's so much better! –  Kromey Apr 7 '11 at 17:20
@Kromey: how would you expect it to work? –  mattdm Apr 7 '11 at 17:57
tail -f just continually streams output to stdout, right? I'd always been under the belief that all Unix redirection operators wait until all the output/input is ready and then move it along, i.e. buffer it all until the sending program/file is done. Thus I wouldn't expect the | in Sean's command here to send anything along to grep until tail is done spitting out lines, which of course with the -f flag it won't ever do until it is interrupted. (I'm not at a *nix box to try this out, though, otherwise I would have just tested it instead of asking.) –  Kromey Apr 7 '11 at 18:03
It works, I use it lots; most of the time to track mail for whiney users. tail -f /var/log/mail.log | grep -i "whineyUser@myDomain.com" –  Sean C. Apr 7 '11 at 18:08
@Kromey, depends on the command after the pipe. If it is sort or wc it will wait the end-of-file to start sorting, if it is grep or sed or another line processing command, it will process input every end-of-line, which is the default character for flushing the i/o stream buffer. –  forcefsck Apr 7 '11 at 19:25
show 1 more comment

use named pipe like this:

file to continously read: logfile.txt

code: (say readfile.sh) while read line do echo $line done < mypipe

In shell:

mkfifo mypipe ./readfile.sh this will block (will not exit)

In anoher shell, go to same directory.

tail -f logfile.txt >> mypipe & this will go to back ground

Done. whatever keeps coming into logfile.txt will get printed via readfile.sh which didn't exit.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.