Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

81%
+7 −0
Q&A grep AND search for multiple words in files

Your grep invocation will first search for files which contain foo and print a list of the lines from each which contain the word foo; the second grep invocation will take this list and filter it d...

posted 11mo ago by Canina‭

Answer
#1: Initial revision by user avatar Canina‭ · 2023-06-09T11:11:57Z (11 months ago)
Your `grep` invocation will first search for files which contain `foo` and print a list of the lines from each which contain the word `foo`; the second `grep` invocation will take this list and filter it down to only those lines of output which also contain `bar`.

A complicating factor is that **XML syntax is relatively complex and does not really lend itself well to line-based utilities** such as `grep`. But ignoring that caveat...

If you want to know which files contain *both* `foo` and `bar` on the same line, you can use a regular expression which will match the two in either order. This is similar to my question [How can I write an egrep (grep -E) regexp that matches lines containing two stanzas in arbitrary order?](https://software.codidact.com/posts/279593), but for something like your use case, one can use for example

    grep -rn '\b(foo|bar)\b.*\b(foo|bar)\b' .

This isn't perfect, since it will match a line containing, say, `foo foo`. (It's probably possible to use negative lookbehind to find only those where the *other* word occurs later.)

A more robust solution for finding those files which actually contain both is to search for one, then search each of those files for the other. For this, grep's `-l`/`--files-with-matches` comes in handy.

    grep -rlw foo .

will print a list of all files under the current directory which contain the word-separated string `foo`. Adding `-Z` will also cause `grep` to print the file names separated by NULL characters instead of newlines, which helps to protect against special characters. Then use `xargs` (and its corresponding `-0` option to expect input entries to be separated by NULLs rather than newlines) to pass that list of files to a second `grep` invocation which looks for `bar` in each of those files:

## Proposed possible solution

    grep -rlwZ foo . | xargs -0 grep -w bar

This should produce a list of all files, and only those files, under the current directory, which contain *both* `foo` and `bar` as whole words, but without regard to their relative location within the file.