Need to search a directories with lots of sub-directories for a string inside files:
I'm using:
grep -c -r "string here" *
How can I total count of finds?
How can I output to file only those files with at least one instance?
Need to search a directories with lots of sub-directories for a string inside files:
I'm using:
grep -c -r "string here" *
How can I total count of finds?
How can I output to file only those files with at least one instance?
I would try a combination of find and grep.
Anyway,
grep -c -r "string here" *
works for me (Mac OS X).Using Bash's process substitution, this gives what I believe is the output you want? (Please clarify the question if it's not.)
This runs
grep -r
normally, with output going both to stdout and to awc -l
process.Some solution with AWK:
Next one is total count, number of files, and number of matches for each, displaying the first match of each one (to display all matches, change the condition to
++f[$1]
):Output for the first solution (searching within a directory for "
boost::
". I manually cut some too long lines so they fit horizontally):Output for the second one
Colors in the result are nice (
--color=always
for grep), but they break when piped through awk here. So better don't enable them then unless you want to have all your terminal colored afterwards :) Cheers!To output only file names with matches, use:
It will output one line with the filename for each file which matches the expression searched for.
Works like a charm.
It works for me (it gets the total number of 'string here' found in each file). However, it does not display the total for ALL files searched. Here is how you can get it:
The list will be in out and the total will be sent to STDOUT.
Here is the output on the Python2.5.4 directory tree:
If you just want to get lines with occurrences of 'string', change to this:
That will output:
You can change how the files ($1) and the count per file ($2) is printed.