Using watch with pipes

I'd like to run this command:

watch -n 1 tail -n 200 log/site_dev.log | grep Doctrine

But it does not run, because "I think" that the grep tries to run on the watch instead of the tail...

Is there a way to do something like

watch -n 1 (tail -n 200 log/site_dev.log | grep Doctrine)

Thanks a lot!


Solution 1:

Surround the command with quotes

watch -n 1 'tail -n 200 log/site_dev.log | fgrep Doctrine'

Solution 2:

I might be wrong, but wouldn't this achieve the same thing (viewing matching log lines as they get added) more simply?

tail -f -n 200 log/site_dev.log | grep Doctrine

Solution 3:

You can surround the command with quotes:

watch -n 1 'tail -n 200 log/site_dev.log | fgrep Doctrine'

If the command has quotes in it, you can use a different type of quotes with appropriate escaping:

watch -n 1 $'tail -n 200 log/site_dev.log | fgrep \'Doctrine.*\''

If you are trying to do something really clever, put the command or commands in a script and use that with watch:

cat <<EOF >/tmp/watch-command
tail -n 200 $(pwd)/log/site_dev.log | fgrep Doctrine
EOF
chmod +x /tmp/watch-command
watch /tmp/watch-command

Make sure to account for relative paths if necessary.