Is there any grep option that let's me control total number of matches but stops at first match on each file?
Example:
If I do this grep -ri --include '*.coffee' 're' .
I get this:
./app.coffee:express = require 'express'
./app.coffee:passport = require 'passport'
./app.coffee:BrowserIDStrategy = require('passport-browserid').Strategy
./app.coffee:app = express()
./config.coffee: session_secret: 'nyan cat'
And if I do grep -ri -m2 --include '*.coffee' 're' .
, I get this:
./app.coffee:config = require './config'
./app.coffee:passport = require 'passport'
But, what I really want is this output:
./app.coffee:express = require 'express'
./config.coffee: session_secret: 'nyan cat'
Doing -m1
does not work as I get this for grep -ri -m1 --include '*.coffee' 're' .
./app.coffee:express = require 'express'
Tried not using grep e.g. this find . -name '*.coffee' -exec awk '/re/ {print;exit}' {} \;
produced:
config = require './config'
session_secret: 'nyan cat'
UPDATE: As noted below the GNU grep -m
option treats counts per file whereas -m
for BSD grep treats it as global match count
So, using
grep
, you just need the option-l, --files-with-matches
.All those answers about
find
,awk
or shell scripts are away from the question.find . -name \*.coffee -exec grep -m1 -i 're' {} \;
find's -exec option runs the command once for each matched file (unless you use
+
instead of\;
, which makes it act like xargs).I think you can just do something like
to e.g. pick the first match from each file, and pick at most two matches total.
Note that this requires your
grep
to treat-m
as a per-file match limit; GNUgrep
does do this, but BSDgrep
apparently treats it as a global match limit.I would do this in
awk
instead.If you didn't need to recurse, you could just do it with awk:
Or, if you're using a current enough bash, you can use globstar:
You can do this easily in perl, and no messy cross platform issues!
The only difference is you have to use Perl style regular expressions instead of GNU style. They're not much different.
You can do the recursive part in Perl using File::Find, or use
find
feed it files.using find and xargs. find every .coffee files and excute -m1 grep to each of them
test without -m1
add -m1