2

According to this thread:

Q: My understanding is that ack uses Perl-derivative regular expressions, is that correct?

A: Yes. In fact, ack is written in Perl and just forwards the expressions to the internal engine.

Now, I have a file under the current directory called:

media_group_xml_producer.py

when I do:

> ack -n '.*group.*producer.*' 

or more directly:

> ack -n --python '.*group.*producer.*' 

I don't get any results. As far as I can tell, the regex should match this file. Why does it miss it?

2 Answers 2

4

If grepping over filenames in the current directory is all you need

ack matches the contents of files, not their name. To find files whose name matches a regex, run ack over the list of filenames:

ls | ack '.*group.*producer.*'

What you did, namely

ack -n '.*group.*producer.*'

will instead look for files in the current directory containing something matching the regex.

If you want to run your regex over full filenames including the subdirectory path

ls won't cut it, because it doesn't have an option to print the path along with the filename. Instead, use tree with flags -i (don't print indentation lines) and -f (print full filenames).

tree -if | ack 'shepherd.*\.pyc?'

returns

./models/shepherd.py
./models/shepherd.pyc
./services/shepherd-related/flock_of_sheep.py
./services/shepherd-related/flock_of_sheep.pyc

All the above works with grep as well as ack, of course. But ack is definitely nicer in every way. :-)

4

You misunderstand what ack does. It is a grep-analog, not a find-analog. It searches through the contents of files for the given pattern and not for file names matching the pattern. You should instead use

find -regex '.*group.*producer.*' 

Or

find -name '*group*producer*' 

If you insist on using ack (though I see no point in doing so), you can parse its output to get your file:

$ ack -f | ack '.*group.*producer.*' 
media_group_xml_producer.py

The ack -f will just print all files that would be searched without actually searching for anything and the second one will print file names that match your regex.

1
  • 1
    ack -f | ack regex is such a common construct that it now has its own flag, -g: ack -g regex. Commented Feb 5, 2015 at 20:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .