Using watch with pipes

189

22

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!

Tommy B.

Posted 2010-05-12T19:23:47.857

Reputation: 2 229

Answers

281

Surround the command with quotes

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

tonylo

Posted 2010-05-12T19:23:47.857

Reputation: 2 926

3

What if the pipeline also contains quotes (such as awk '{print $3}')? Edit: Like this

– OrangeDog – 2018-06-06T16:28:22.207

3you can escape those chars with \ , ie watch -n 'awk \'{print $3}\'' – lev – 2018-09-21T10:44:07.873

31

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

Mitch

Posted 2010-05-12T19:23:47.857

Reputation: 849

1

No, I think you're confusing the means and the end. The user clearly wanted to see Doctrine arriving in a growing file, and when he looked in his toolbox, the only thing he found was watch. What he really needed to know was tail -f. See also http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem

– dland – 2016-03-21T11:03:14.243

11

I think these are both acceptable answers. The top and accepted answer correctly answers the exact question posed, and this answer correctly identifies the XY problem that OP created for themselves and provides the solution they really wanted in the first place. Both answers could easily be useful to someone coming across this question.

– cdhowie – 2016-04-14T16:14:18.163

2I was looking for a way to watch shellcheck *.sh | grep line | wc -l and the accepted answer was useful for me. – Amedee Van Gasse – 2017-01-03T12:29:44.017

8I agree this may be more efficient as far as CPU is concerned, but in the context of the topic "Using watch with pipes" it doesn't use watch so isn't an answer. This might be a case of a poor example question as watch and pipes seem to appear frequently not in the context of tail. – tu-Reinstate Monica-dor duh – 2014-01-20T00:07:33.307

4

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.

bschlueter

Posted 2010-05-12T19:23:47.857

Reputation: 168

0

Use double quotes, as watch is executing the command:

watch "ls -la | wc -l"

Robert Blandford

Posted 2010-05-12T19:23:47.857

Reputation: 1