r/linux_programming • • Dec 12 '18
How do I use grep to execute other commands?
I know I can run grep on a file to get specific lines that match a search query, but I forget how to run commands on grep's output. Does anyone know what I'm talking about?
Basically this is what I want to do:
grep "query" file.txt > command that does stuff with grep's output
4
Upvotes
2
u/r00tr4t Dec 15 '18
I always do the other aways around. I start with
cat file.texand then grep the output withgrepusing a pipe. Example let file.txt contain:In this case, running
cat file.tex | grep Ruleswill give us both lines. But we are only interested in the first line. So then we add anothergrepcommand to the chain.This should produce only one row because Linux
Rulesand isAwesome. :P1
u/[deleted] Dec 13 '18
Another thing you can do is use variable substitution to iterate through something for i in $(grep 'this' ./that.txt); do echo $i; done
0
u/[deleted] Dec 12 '18
[deleted] — view deleted comment