How to match once per file in grep?

后端 未结 6 1107
再見小時候
再見小時候 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:09

    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.

提交回复
热议问题