How to get ripgrep to tell me which expressions from a list have no matches on the filesystem

867 Views Asked by At

For instance, say I have the list of strings that I want to search for:

alfa bravo charlie delta nebuchadnezzar bartholomew

and in my repo there are files that contain alfa, bravo, charlie and delta, but there are no files that contain nebuchadnezzar and no files that contain bartholomew. Then I want the answer to be:

nebuchadnezzar bartholomew

As you might guess, I'm searching for deprecated things. I ended up using the following Ruby code workaround as I couldn't figure a solution after trying man rg.

%w[alfa bravo charlie delta nebuchadnezzar bartholomew].each do |word|
  command = 'rg ' + word
  if `#{command}` == ''  # execute the command, see if ripgrep found nothing
    puts word
  end
end
1

There are 1 best solutions below

1
On

You can use the exit code of rg when no match is found in a simple shell loop construct. From the docs, it seems it returns a code 1 when no match is found for the regex and no errors are seen. Adopting it

for word in alfa bravo charlie delta nebuchadnezzar bartholomew; do
    rg "$word" >/dev/null 2>&1
    [ "$?" -eq 1 ] && printf '%s\n' "no match for $word"
done