0

Given multiple strings to find with ack, is there a way/option/flag that will return the strings that were not found?

I.e., in this situation, I want "dog" to be output, as it's not found in the file

touch myFile.txt
echo 'hello, world' > data.txt 

ack "hello|world|dog"

However, "hello, world" is found

m19v
  • 1,800
  • 4
  • 11
  • 26
Chris
  • 11
  • 4
  • 1
    While I never have worked with `ack`, I don't think that this can be done out of the box. Actually, with your pattern, `ack` would never search for either _world_ nor _dog_, since _hello_ already matches; therefore ack itself does not know that your dog is missing, so it can't tell you either. I think you would have to write your program to achieve what you want. – user1934428 Oct 06 '22 at 12:02
  • This is interesting, as this SO post suggests both strings can be found: https://stackoverflow.com/questions/26275582/searching-multiple-patterns-words-with-ack – Chris Oct 06 '22 at 13:28
  • The pattern `x|y` matches, if either _x_ or _y_ is found (provided that **extended** regular expressions are used). This means that if the parser finds one of the strings, it is done. Searching for a second match in the same line would only waste CPU cycles. Of course things are differently when you also use the `-o` option of `grep`, because grep then tries tor produce as many matches as possible. In this case, the parser would try another match in the same line. – user1934428 Oct 07 '22 at 07:20

1 Answers1

0

ack, like many search tools (grep, ag, ripgrep), only prints output from the source file (in this case, data.txt), not the pattern itself. Since the pattern 'dog' doesn't exist in the source file, it will not print regardless of the pattern you use.

You may be thinking of ack's flag --invert-match or grep's similar flag -v, but these flags only print content not matching the pattern. Again, since 'dog' is not in the file, it won't print: inverted flags do not print the pattern, but what content is searched for in the file.

gregory
  • 10,969
  • 2
  • 30
  • 42