Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on grep AND search for multiple words in files
Parent
grep AND search for multiple words in files
I have text (xml actually) files. Some files contain 'foo', some contain 'bar', some contain neither and some contain both. It's the both I'm interested in.
How do I do an AND search on words in files in folders (recursively) with grep? I'm using git bash in windows, so either a cygwin or win10 solution works.
I had thought pipe
ing to grep would work as it seems to be the solution for multiple text on a line, but I don't think I've changed it to work for files correctly.
This is what I tried:
$ grep -rnw . -e 'foo' | grep -e 'bar'
Can someone tell me how to fix my grep call?
Between-lines relations are not easy to look for with grep, which is a line filter. You could use a regex that spans lin …
1y ago
Your `grep` invocation will first search for files which contain `foo` and print a list of the lines from each which con …
1y ago
From your description ... > I have text (xml actually) files. Some files contain 'foo', some contain 'bar', some con …
1y ago
An alternative to `grep` is Awk, which makes this pretty easy. To find lines which contain both: ``` find . -type …
1y ago
You can utilize grep's PERL regexes, more specifically lookarounds, to check presence of two (or more) words. ```sh …
1y ago
Post
Between-lines relations are not easy to look for with grep, which is a line filter. You could use a regex that spans lines, but I find this annoying because of all the flags you have to set.
Grep has a switch for printing the filenames instead of matching lines. You can put each in a file. Once you have both files, you can use comm
to do the union.
grep -r . -e 'foo' > foo.txt
grep -r . -e 'bar' > bar.txt
comm foo.txt bar.txt -12
If you don't want the temp files, you can use the command inline: https://linux.codidact.com/posts/288328/288329#answer-288329 However, I simply put the files in /tmp/
where they get automatically wiped at next system shutdown.
0 comment threads