tail -f doesn't give single lines when piped through grep

Launching a website and i wanted to setup a a Bash one liner so when someone hits the site it would make a beep using the internal buzzer.

So far its working using the following.

tail -f access_log | while read x ; do echo -ne '\007' $x '\n' ; done

Tail follows the access_log and dumps to STDOUT, get STDOUT line at a time, echo the line with '\007' "internal beep hex code", done..

This works like a beauty.... Every hit shows the line from the log and beeps.. However it got annoying very quickly, so ideally i wanted to filter the tail -f /access/log before its piped into the while so that read only gets lines i care about i was thinking grep "/index.php" would be a good indication of visitors..

This is where the issue is...

I can do...

tail -f access_log | while read x ; do echo -ne '\007' $x '\n' ; done

beeps on everything and i can do...

tail -f access_log | grep "/index.php"

and pages are shown with no beep, but when i do

tail -f access_log | grep "/index.php" | while read x ; do echo -ne '\007' $x '\n' ; done

Nothing happens, no line from log, no beep.

I think the grep is messing it up somewhere but i cant figure where. I'd love it to be a one liner, i know it should really be done in a script and would be easier, but it doesn't explain why the above, which i think should work, isn't.

Answers


Grep's output is buffered when it's used in a pipe. Use --line-buffered to force it to use line buffering so it outputs lines immediately.

tail -f access_log | grep --line-buffered "/index.php" | while read x ; do echo -ne '\007' $x '\n' ; done

You could also combine the grep and while loop into a single awk call:

tail -f access_log | awk '/\/index.php/ { print "\007" $0 }'

Need Your Help

How do I force Windows Phone to not recolor my App Bar Icons?

.net windows-phone-7 xaml windows-phone-8

I have an app I am developing and the entire theme is custom totally separate from whatever theme the user has chosen on their device. Is there a way to prevent Windows Phone from recoloring the