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
Some solution with AWK:
grep -r "string here" * | awk 'END { print NR } 1'
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]
):
grep -r "string here" * |
awk -F: 'END { print "\nmatches: ", NR, "files: ", length(f);
for (i in f) print i, f[i] } !f[$1]++'
Output for the first solution (searching within a directory for "boost::
". I manually cut some too long lines so they fit horizontally):
list_inserter.hpp: return range( boost::begin(r), boost::end(r) );
list_of.hpp: ::boost::is_array,
list_of.hpp: ::boost::decay,
list_of.hpp: ::boost::decay >::type type;
list_of.hpp: return ::boost::iterator_range_detail::equal( l, r );
list_of.hpp: return ::boost::iterator_range_detail::less_than( l, r );
list_of.hpp: return ::boost::iterator_range_detail::less_than( l, r );
list_of.hpp: return Os << ::boost::make_iterator_range( r.begin(), r.end() );
list_of.hpp: return range( boost::begin(r), boost::end(r) );
list_of.hpp: return range( boost::begin(r), boost::end(r) );
list_of.hpp: return range( boost::begin(r), boost::end(r) );
ptr_list_of.hpp: BOOST_DEDUCED_TYPENAME boost::ptr_...
ptr_list_of.hpp: typedef boost::ptr_vector impl_type;
13
Output for the second one
list_inserter.hpp: return range( boost::begin(r), boost::end(r) );
list_of.hpp: ::boost::is_array,
ptr_list_of.hpp: BOOST_DEDUCED_TYPENAME boost::ptr_...
matches: 13 files: 3
ptr_list_of.hpp 2
list_of.hpp 10
list_inserter.hpp 1
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!