How can I show ack results and number of occurrences

ack foo * 

returns a list of strings:

 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 need a list of lines, as in the first example, and a count of the number of lines matching in the second example.

+7
source share
2 answers

you can use

ack -hc ( -h is short for --no-filename ) to get the total.

According to the documentation / man page ack :

-c, --count

Suppress normal output; instead, print the number of matching lines for each input file. If -l is valid, it will only show the number of lines for each file that has the corresponding lines. Without -l some line counts can be zeros.

When combined with -h ( --no-filename ) ack only one total is output.

Here's what worked for me (expanded 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, returning to start; use ; instead, it will run them both, one after the other, independently. In this case, I think that ; more appropriate.

+8
source

It is not possible to get ack to output both things that you want in the same command. The easiest way to get what you want is to link both teams with && .

+2
source

All Articles