سؤال

Oddness in ack (betterthangrep.com) - Sometimes I get a line number, sometimes not.

In the first example, I look for a simple phrase in some Python files. It works fine, and shows line number 233. In the second example, I look for all lines where the first column ends with '5'.

D:\DSWProj\2012L\src> perl D:\ack-standalone.pl  "return True"  *.py
Volume5.py
233:    return True


D:\DSWProj\2012L\data\R123> perl D:\ack-standalone.pl  "5 "  NeedMoreCoffee_n5_*.data
Volume5 FooData  coffee_n5  2013-01-29 10:14
 397125 0 0 1 0
 397135 0 0 1 0

Those two lines are exactly the first two lines in the file following the header line.

Why are there no line numbers in this second example? There's also no file name printed, and no colorization. The Python example does colorize output - the line numbers are yellow, file names green. (A surprise, since the last time I asked, was told Windows command line shell can't colorize anything at all.) Is this a bug in ack, or quirk of Windows? Did I do something unwittingly different between the two cases, besides the obvious searching for different things in different files?

Running on Windows 7 64-bit, using Strawberry Perl 5.16.2 downloaded two days ago (January 28th, 2013), and ack standalone script, ver 1.96, downloaded also two days ago.

هل كانت مفيدة؟

المحلول

I believe ack magically* knows that *.py files are Python source code, and will show line numbers and colorization for source code files but not for plain old data files like *.data. This is considered a feature, not a bug.

If you want a predictable, composable replacement for grep, try grep. ;)

* – It's not really magic, of course; it's configurable in your .ackrc file. Run ack --help-types for more information.

EDIT: I'm wrong! It's not because of the difference between .py and .data; it's because of the difference between ack pattern f1 and ack pattern f1 f2 .... In the latter case, ack will print which of the files f1 f2 ... contained the matches; in the former (single file) case, ack will behave more like grep.

To fool ack into producing line numbers even for a single file, add a fake file on the end of the line. For Cygwin, you could probably do

perl D:\ack-standalone.pl  "5 "  NeedMoreCoffee_n5_*.data /dev/null

For Windows, maybe this still works:

perl D:\ack-standalone.pl  "5 "  NeedMoreCoffee_n5_*.data NUL

(that's literally the letters N U L, which is how Windows spells /dev/null; see also CON, LPT1, etc).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top