How to output the shell find result and the match number?

306 Views Asked by At

I want to use find command to find some string in files, and want to see the find result and the number of match line at the same time.

For example, use following command, I can get the find result, but I cannot know how many match result I have got.

find . -name .history  | xargs grep ls

the output is

ls -r
ls -R
ls

what I expect output is like this

ls -r
ls -R
ls
total match number: 3

How can I get my expect output?

2

There are 2 best solutions below

1
Ignacio Vazquez-Abrams On BEST ANSWER
.... | awk '{ a+=1; print} END { print "total match number: " a}'
0
Bernhard On

An alternative without awk is:

| tee >(cat) >(wc -l)

To give you the output and the number. If you really want the text also:

| tee >(cat) >(echo "total match number: "`wc -l`);