I'm attempting to find the number of matches for a given string across a large project. Currently, to do this with ag
I am using the following command:
$ echo 0$(ag -c searchterm | sed -e "s/^.*:/+/") | bc
which is obviously a bit lengthy and not very intuitive. Is there any better way to get the total number of matches in a directory from ag
? I've dug through the documentation and couldn't find anything helpful there.
Edit: Thanks to a recent commit to ag
, the filenames can be stripped with ag
instead of sed
, so this also works:
$ echo `ag test -c --nofilename | sed "s/$/+/"`0 | bc
Note: I realize I could do this with ack -hcl searchterm
(Well, almost. In my specific case I'd need an --ignore-dir building
in there as well), but as this is already a large project (and will be growing considerably), the speed boost offered by ag
makes it preferable (ack
takes about 3 seconds for my searches vs ag
's nearly instantaneous result), so I would like to stick with it.
--stats
option followed by parsing out the correct line. (e.g.,ag --stats searchterm | tail -n 5 | head -n 1
). I also submitted a pull request for a--stats-only
option which prevents anything else from being printed, in that caseag --stats searchterm | head -n 1
would get the number of matches. In both those cases you'd still need to filter out " matches" to get just the number though. – Paolapaolina