2

I just want to share this, and hoping will get more similar tool.

I got a directory of text files.. i need to search for the files with 2 (partial) words in it.. the keywords are in random order, but on a same line.

So far, i can use this:

$ rg -i '\w*keyword1\w*.*\w*keyword2\w*|\w*keyword2\w*.*\w*keyword1\w*' --glob '*.txt' --line-number -l /path/to/search

$ rg -g '*.txt' -l -i 'keyword1.*keyword2|keyword2.*keyword1' /path/to/search

Both of these can do the same job.

Anybody have other that can do the same job ? Pls do share. I found myself need this quite often to locate the proper file..

However i don't know how to search file that filename is 2 keywords that are not in order. How about searching for filename with 2 keyword that is not in order ? Example: weirdfilename_key1Nkey3Nkey2etc.txt search among huge database for file with which name only remember consist of "key2" and "key3" in it, but not sure the order..

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
andrew_ysk
  • 155
  • 5

1 Answers1

0

Maybe:

rg -iPlg '*{Key1*Key2,Key2*Key1}*.txt' '^(?=.*keyword1)(?=.*keyword2)' dir

With -P, we get PCRE regexps that have look ahead operators so we can match ^ the start of the line provided it's followed by any number of characters followed by keyword1 and that it's also followed by any number of characters followed by keyword2.

-g '*{Key1*Key2,Key2*Key1}*.txt' is short for -g '*Key1*Key2*.txt -g '*Key2*Key1*.txt' where that {x,y} similar to csh brace expansion is a ripgrep extension to standard shell glob patterns.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501