Using watch with pipes

51,906

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.

Share:
51,906

Related videos on Youtube

Shawellaby
Author by

Shawellaby

πŸ™‹πŸΌβ€β™‚οΈ I'm a full-time freelance developer and open-source contributor who've been building mobile, web and software solutions for the past 15 years in multiple industries including the Medical/Health Industry, Adult Industry, Social Media, SaaS, VoIP, VPN, Web Hosting, E-Commerce &amp; Marketing, ERPs, Automobile, Metallurgy and more. 🌟 Expertise areas: PHP, NodeJS, ExpressJS, C#, Visual Basic, MySQL, PostgreSQL, SQL Server, MongoDB, REST APIs, Heroku, DigitalOcean, AWS (Lambda, EC2, S3, RDS and more), VueJS, React, Angular, Ionic, TypeScript, Bootstrap, SCSS and more. πŸ‘¨β€πŸ’» Why choose me? I'm always available and extremely responsive. That's what passion is! I focus not only on delivering value, but I also improve all the projects I touch by bringing suggestions and ideas to every client I work with. I encourage you to take a look at my work history and other related profiles. (I'm active on GitHub, StackOverflow and LinkedIn) Contact me at anytime, thanks for checking out my profile! πŸ‘‹

Updated on September 17, 2022

Comments

  • Shawellaby
    Shawellaby over 1 year

    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!

  • dland
    dland about 8 years
    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 meta.stackexchange.com/questions/66377/what-is-the-xy-probleβ€Œβ€‹m
  • cdhowie
    cdhowie about 8 years
    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.
  • Amedee Van Gasse
    Amedee Van Gasse over 7 years
    I was looking for a way to watch shellcheck *.sh | grep line | wc -l and the accepted answer was useful for me.
  • OrangeDog
    OrangeDog almost 6 years
    What if the pipeline also contains quotes (such as awk '{print $3}')? Edit: Like this
  • lev
    lev over 5 years
    you can escape those chars with \ , ie watch -n 'awk \'{print $3}\''
  • Admin
    Admin almost 2 years
    For more information on the difference between '…' and $'…'. stackoverflow.com/questions/11966312/…