Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show ack results and occurrence count

Tags:

linux

ack

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.

like image 693
Chris Avatar asked Nov 14 '12 06:11

Chris


1 Answers

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.

like image 180
Aaron Wallentine Avatar answered Oct 02 '22 13:10

Aaron Wallentine