How to match once per file in grep?

后端 未结 6 1194
再見小時候
再見小時候 2021-02-06 20:38

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 \'*

6条回答
  •  面向向阳花
    2021-02-06 21:02

    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'
    ./test1.txt:oyss
    ./test1.txt:oyss1
    ./test1.txt:oyss2
    ./test2.txt:oyss1
    ./test2.txt:oyss2
    ./test2.txt:oyss3
    

    add -m1

    linux# find . -name '*.txt'|xargs grep -m1 -ri 'oyss'
    ./test1.txt:oyss
    ./test2.txt:oyss1
    

提交回复
热议问题