How to match once per file in grep?

Is there any grep option that let's me control total number of matches but stops at first match on each file?


If I do this grep -ri --include '*.coffee' 're' . I get this:

./ = require 'express'
./ = require 'passport'
./ = require('passport-browserid').Strategy
./ = express()
./    session_secret: 'nyan cat'

And if I do grep -ri -m2 --include '*.coffee' 're' ., I get this:

./ = require './config'
./ = require 'passport'

But, what I really want is this output:

./ = require 'express'
./    session_secret: 'nyan cat'

Doing -m1 does not work as I get this for grep -ri -m1 --include '*.coffee' 're' .

./ = 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


I think you can just do something like

grep -ri -m1 --include '*.coffee' 're' . | head -n 2

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; GNU grep does do this, but BSD grep apparently treats it as a global match limit.

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.

I would do this in awk instead.

find . -name \*.coffee -exec awk '/re/ {print FILENAME ":" $0;exit}' {} \;

If you didn't need to recurse, you could just do it with awk:

awk '/re/ {print FILENAME ":" $0;nextfile}' *.coffee

Or, if you're using a current enough bash, you can use globstar:

shopt -s globstar
awk '/re/ {print FILENAME ":" $0;nextfile}' **/*.coffee

using find and xargs. find every .coffee files and excute -m1 grep to each of them

find . -print0 -name '*.coffee'|xargs -0 grep -m1 -ri 're'

test without -m1

linux# find . -name '*.txt'|xargs grep -ri 'oyss'

add -m1

linux# find . -name '*.txt'|xargs grep -m1 -ri 'oyss'

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).

You can do this easily in perl, and no messy cross platform issues!

use strict;
use warnings;
use autodie;

my $match = shift;

# Compile the match so it will run faster
my $match_re = qr{$match};

FILES: for my $file (@ARGV) {
    open my $fh, "<", $file;

    FILE: while(my $line = <$fh>) {
        chomp $line;

        if( $line =~ $match_re ) {
            print "$file: $line\n";
            last FILE;

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.

find /some/path -name '*.coffee' -print0 | xargs -0 perl /path/to/your/program

Need Your Help

Get Value of Radio button group

jquery radio-button

I'm trying to get the value of two radio button groups using the jQuery syntax as given below. When the code below is run I get the value selected from the first radio button group twice instead of

Plotting with a transparent marker but non-transparent edge

python numpy matplotlib plot transparency

I'm trying to make a plot in matplotlib with transparent markers which have a fixed color edge . However, I can't seem to achieve a marker with transparent fill.