Is is possible to filter the output of tail?
I'd like to tail a file but only output lines that have a certain string in them. Is this possible?
use grep. Its built just for that purpose.
To find lines from a tail of /var/log/syslog that have "cron" in them, just run:
tail -f /var/log/syslog | grep cron
And since it accepts anything over stdin, you can use it on the output of any other command as well, by piping in the same way as above (using the | symbol).
tail -f /var/log/messages | grep "myfilterword"
Hope that helps.
Here's a couple other ideas, which while not as simple, may offer some interesting additional flexibility:
First, you can filter with awk instead of grep:
tail -f /var/log/messages | awk '/myfilterword/'
that works exactly the same as the example using grep
. You can expand on this by using the power of awk, for example:
tail -f /var/log/messages | \
awk '/myfilterword/ { for (i=6; i<=NF; i++) printf("%s ", $i); printf("\n")}'
which will print the 6th through the last field of the output (fields are whitespace separated)
Another similar idea is to use a perl one-liner:
tail -f /var/log/messages | perl -ne "/myfilterword/ and print"
that works exactly like grep
. Maybe you want a line number counter and just the 6th field? How about this:
tail -f /var/log/messages | \
perl -lane "/myfilterword/ and printf \"%6d %s\n\",++\$a,\$F[6]"
Obviously all of these sorts of things can be done with other tools too, but I wanted to illustrate that there are some fun ways to use more general purpose languages like awk or perl here.