Using watch with pipes
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.
Related videos on Youtube
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 & 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, 2022Comments
-
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 about 8 yearsNo, 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 waswatch
. What he really needed to know wastail -f
. See also meta.stackexchange.com/questions/66377/what-is-the-xy-probleββm -
cdhowie about 8 yearsI 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 over 7 yearsI was looking for a way to watch
shellcheck *.sh | grep line | wc -l
and the accepted answer was useful for me. -
OrangeDog almost 6 yearsWhat if the pipeline also contains quotes (such as
awk '{print $3}'
)? Edit: Like this -
lev over 5 yearsyou can escape those chars with \ , ie
watch -n 'awk \'{print $3}\''
-
Admin almost 2 yearsFor more information on the difference between
'β¦'
and$'β¦'
. stackoverflow.com/questions/11966312/β¦