0

Similar to this question How do I find all files containing specific text on Linux? but I want all the files that contain multiple given strings (these strings not necessarily next to each other or on the same line, just in the same file).

My use case is I am looking at a UI and want to modify the file which controls this particular screen. The codebase though is huge and it is difficult to locate this file. All I have to go on is some of the hardcoded strings on this screen which I would like to do the search on. The strings are quite generic though such as 'Done', 'Close', 'View Details'... Doing a search on any of these strings individually, using the answer from the linked question above, brings back too many results but I think doing the search on all of them together will filter it down enough to find the file.

user2802557
  • 747
  • 1
  • 9
  • 19
  • Maybe use multiple `-e` options to `grep`? –  May 21 '20 at 14:03
  • There is a similar question [here](https://stackoverflow.com/questions/44209441/recursively-find-files-that-contain-all-the-given-patterns). Is this what you're looking for? – oguz ismail May 21 '20 at 14:06
  • @Roadowl That'd correspond to logical OR per line and wouldn't return files that contain all strings. – Benjamin W. May 21 '20 at 14:17
  • @Benjamin W "contain multiple given strings" isn't necessarily a paraphrase of `AND`. –  May 21 '20 at 14:46
  • if you are looking to get files that contain more than one string, use something like `grep -rlZ 'Done' | xargs -0 grep -lZ 'Close' | xargs -0 grep -l 'View Details'` where `-r` will do recursive search (current directory here as no specific path is passed).. keep adding `xargs -0 grep -lZ` to as many strings you need – Sundeep May 21 '20 at 15:01
  • @Roadowl I interpreted "too many results but I think doing the search on all of them together" as "contains all of them". – Benjamin W. May 21 '20 at 15:22
  • @Sundeep this works giving the result I need though was also very slow, I expect because of having do multiple greps, if there's a way to do it with one I think would be much faster – user2802557 May 21 '20 at 15:34
  • are they are ASCII files? if so, using `LC_ALL=C grep ....` will be much faster... otherwise, I'd suggest [ripgrep](https://github.com/BurntSushi/ripgrep) as it automatically makes use of multiple cores/threads/etc for better performance and has other optimizations as well.. – Sundeep May 21 '20 at 15:41
  • using single grep may work if you have PCRE (which is likely as you are on linux).. but it comes with complex regex and it depends on your input.. anyway, do try `grep -rlzP '(?s)(?=.*Done)(?=.*Close)(?=.*View Details)'` – Sundeep May 21 '20 at 15:44
  • I believe Sundeep's solution is the most efficient approach. However: put the search term you expect to match the *fewest* files first; the second, third etc. applications of `grep` don't look at **all** your files, they only look at the files that are already known to contain the first search term (or all the previous ones, in general). Also, if you are using fixed strings, use the `-F` flag to do only fixed-string searches (much faster). And, if you want to search for whole words only (`'Close'` good, `'Closest'` no match) then add the `-w` flag as well. Tell us how it goes. –  May 21 '20 at 23:39

0 Answers0