سؤال

ack foo *

returns a listing of lines:

bar.txt
28: this is foo stuff

dump.txt
12: results of foo

gobs.txt
1137: more lines with foo

and

ack -c -l 

returns

3

My question is, how can I show both? I'd like the list of lines as in the first example and a count of the number of lines that matched as in the second example.

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

المحلول

You can use

ack -hc (-h being shorthand for --no-filename) to get a total count.

According to the ack documentation/man page:

-c, --count

Suppress normal output; instead print a count of matching lines for each input file. If -l is in effect, it will only show the number of lines for each file that has lines matching. Without -l, some line counts may be zeroes.

If combined with -h (--no-filename) ack outputs only one total count.

Here's what worked for me (expanding on @Jordan's answer) --

ack 'pattern' && ack -hc 'pattern'

or, better (IMO):

ack 'pattern'; ack -hc 'pattern'

As far as I understand, using &&, the second command depends on the first one returning to run; using ; instead will just run them both, one after the other, regardless. In this case, I think the ; is more appropriate.

نصائح أخرى

There's no way to get ack to output both of the things you want in a single command. The easiest way to get what you want is to chain both commands together with &&.

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