37

I have to search trough a huge amount of .txt files. I know I can do multiple words search, but here's my problem. I want my results to only be the .txt files that contain all of the words of the search entry.

For example:

File 1:

test1

File 2:

 test1
 test2

Search files for test1 and test2 will only result in showing File 2.

Hope someone can help me out. Thanks

perror
  • 7,071
  • 16
  • 58
  • 85
Steelduines
  • 371
  • 1
  • 3
  • 3
  • How many words are in your search entry? You could use grep: http://stackoverflow.com/a/5915770/1423890 – BunjiquoBianco Sep 04 '12 at 16:15
  • 1
    Do you want to identify files that contain all the words, or do you want to find lines that contain all the words? It is a simple task in Perl, something like `perl -ne "print if m/one/ && m/two/ && m/three/" *.txt`. – AdrianHHH Mar 29 '13 at 19:09

3 Answers3

58

If you are using Notepad++ editor (like the tag of the question suggests), you can use the great "Find in Files" functionality.

Go to Search > Find in Files (Ctrl+Shift+F for the keyboard addicted) and enter:

  • Find What = (test1|test2)
  • Filters = *.txt
  • Directory = enter the path of the directory you want to search in. You can check Follow current doc. to have the path of the current file to be filled.
  • Search mode = Regular Expression
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
  • Is there a way to use a list of strings in an input file as "Find What" and search in list of files under a folder. The output would be list of files against each string in an input file. – Jit May 25 '18 at 18:57
  • Not with Notepad++. A few lines of script (Python or Perl) would do the job though. Tell me if you need help writing it – Jean-Francois T. May 25 '18 at 23:32
  • 1
    Surely this would find File 1 as well when it shouldn't? – Lilienthal Jan 07 '19 at 14:58
  • @Lilienthal what do you mean? – Jean-Francois T. Jan 07 '19 at 15:01
  • 2
    @Jean-FrancoisT. The OP is asking for a way to search for files containing both test1 and test2 somewhere in them. File 1 only contains test1 and shouldn't be a hit, but with `|` it is. I suppose `((test1.*test2)|(test2.*test1))` might work but that doesn't really scale well. – Lilienthal Jan 08 '19 at 12:32
  • flawless answer!! – Gaurav Jan 24 '19 at 09:40
  • @Lilienthal I guess I got confused by the sentence "*Search files for test1 and test2 will only result in showing File 2.* " ... using "**shall**" there would have avoided any confusion. Indeed, the regex you suggest is what he needs but does not scale well... For complex stuff, few lines of Perl or Python would do the trick better. On the other hand, the OP did not manifest much so I will leave my solution like that :) – Jean-Francois T. Jan 24 '19 at 10:44
  • @Jean-FrancoisT. No problem, mainly pointed it out because I've been looking for the same thing as the OP and have run into a dead end with Notepad++. A solution to this would involve a more advanced search tool probably. – Lilienthal Jan 24 '19 at 12:35
5

If you are using Notepad++ editor Goto ctrl + F choose tab 3 find in files and enter:

  1. Find What = text1*.*text2
  2. Filters : .
  3. Search mode = Regular Expression
  4. Directory = enter the path of the directory you want to search in. You can check Follow current doc. to have the path of the current file to be filled.
DavidG
  • 113,891
  • 12
  • 217
  • 223
HungSmile
  • 61
  • 1
  • 3
  • 1
    This won't work if `text1` comes after `text2`, that's why the original question has a better Regex. – DavidG Oct 16 '18 at 08:41
  • Also, I fixed your formatting, please take note next time you post of how the formatting works on Stack Overflow. – DavidG Oct 16 '18 at 08:42
  • yes but you can change Find What = text2*.*text1. I Think its true better text1|text2 – HungSmile Oct 16 '18 at 08:43
  • 1
    yes i see. it the first time i answer question on stackoverflow – HungSmile Oct 16 '18 at 08:45
  • If you change it, it won't work for the other way round. The other answer is the only one that makes sense. – DavidG Oct 16 '18 at 08:45
  • The `1*.*` part of your answer should be, I believe, `1.*`. The `1*.*` means zero or more `1`s then zero or more other characters and a simple `.*` has the same effect. But that would also match against `test3xxxtest2` which is not wanted. – AdrianHHH Oct 16 '18 at 08:49
-1

It is another "if you use X" solution, but if you use GitHub, you can use search bar to find code in your repository. It will then list your files matching that code part.

Toma400
  • 21
  • 1
  • 6
  • 1
    Can you please explain how to do the search as wanted by the question. Just saying *"use GitHub"* does not answer the question. – AdrianHHH Oct 12 '21 at 08:59
  • I thought it's really straightforwardly shown in GitHub itself, this is why I didn't explain it clearer. If you open your own GitHub repository (or someone elses), then you will see search bar on left top side (or middle-right top side, if not logged in). Here you write code part you want to search, and from menu appearing after that you choose "in this repository". It redirects you to site with listed elements of your repository, first being code (and files where searched code appear). Only limit I found: you can't search for longer code fragments, so it limits you to single line. – Toma400 Oct 12 '21 at 18:30
  • The original question is not a general how to do a search, it is about how to search for a specific set of textual items. That is why the question is interesting. Your answer completely avoids the point of the original question. – AdrianHHH Oct 12 '21 at 20:15