How can I show ack results and occurrence count
Asked Answered
A

2

17
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.

Amphibian answered 14/11, 2012 at 6:10 Comment(2)
You can't. And, in ack 2.0, it will explicitly tell you "You can't use -c and -l together".Castle
Ack 2.12 lets me use -c and -l together. It displays a list of the names of matched files and the number of matches in each file. As far as I can see, you still can't do what OP is trying to do though.Refugio
C
26

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.

Cavallaro answered 15/7, 2016 at 4:24 Comment(1)
Great answer, I was looking for this for a long time.Medici
E
2

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 &&.

Entrench answered 14/11, 2012 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.