How to match once per file in grep?

后端 未结 6 1108
再見小時候
再見小時候 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 20:54

    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
    

提交回复
热议问题