What's the ack syntax to find all files containing foo and also bar?
1 Answer
First we need to select the files that contain foo and, from that list of files, selects files that also contain bar. Thus:
ack -l foo | ack -x bar
As you can see, this works in two steps:
ack -l fooproduces a list of the names of files that containfoo.ack -x barsearches the files named on stdin for the expressionbar.
-
Shouldn't that first bit be
ack -l foo *?Chris Davies– Chris Davies2016-09-01 22:43:47 +00:00Commented Sep 1, 2016 at 22:43 -
1@roaima If no files or directories are given on the command line,
ackdefaults to searching recursively starting with the current directory. This default is similar togrep -r pattern. Your approach. of course, would work also.John1024– John10242016-09-01 22:54:16 +00:00Commented Sep 1, 2016 at 22:54 -
1Thank you. It's a new command to me, but the man page seemed to jump on the "it's a drop-in for
grep" line. (I'll delete this later. Or maybe you can.)Chris Davies– Chris Davies2016-09-01 22:56:15 +00:00Commented Sep 1, 2016 at 22:56 -
@roaima Yes, it is similar but different. To make it more like
grep, I have a~/.ackrcfile that contains--no-recurse. (I can, of course, override that on the command line withack -r.)John1024– John10242016-09-01 22:59:22 +00:00Commented Sep 1, 2016 at 22:59
ackis (almost) a drop-in replacement forgrep, the answers in How to search files where two different words exist? should work here too.