You can add an extra call -execto grep, and then pass the number of matches in awkinstead of the file name:
#!/usr/bin/env bash
find /path/to/dir \
-type f \
-name '*.txt' \
-exec grep -c 'replaceWhat' '{}' \; \
-exec perl -e 's/replaceWhat/replaceWith/ig' -pi '{}' \; \
| awk '{count += $0 }; END { print count " replacement(s) made" }'
echo "Done!"
Example (replacing "before" with "after"):
$ tail -n +1 *.txt
==> 1.txt <==
before
foo
bar
==> 2.txt <==
foo
bar
baz
==> 3.txt <==
before
foo
before
bar
before
$ ./count_replacements.sh
4 replacement(s) handled
$ tail -n +1 *.txt
==> 1.txt <==
after
foo
bar
==> 2.txt <==
foo
bar
baz
==> 3.txt <==
after
foo
after
bar
after
source
share