How do I limit the number of results returned from grep?

182,199

Solution 1

The -m option is probably what you're looking for:

grep -m 10 PATTERN [FILE]

From man grep:

-m NUM, --max-count=NUM
        Stop reading a file after NUM matching lines.  If the  input  is
        standard  input  from a regular file, and NUM matching lines are
        output, grep ensures that the standard input  is  positioned  to
        just  after the last matching line before exiting, regardless of
        the presence of trailing context lines.  This enables a  calling
        process  to resume a search.

Note: grep stops reading the file once the specified number of matches have been found!

Solution 2

Another option is just using head:

grep ...parameters... yourfile | head

This won't require searching the entire file - it will stop when the first ten matching lines are found. Another advantage with this approach is that will return no more than 10 lines even if you are using grep with the -o option.

For example if the file contains the following lines:

112233
223344
123123

Then this is the difference in the output:

$ grep -o '1.' yourfile | head -n2
11
12

$ grep -m2 -o '1.'
11
12
12

Using head returns only 2 results as desired, whereas -m2 returns 3.

Solution 3

Awk approach:

awk '/pattern/{print; count++; if (count==10) exit}' file

Solution 4

For 2 use cases:

  1. I only want n overall results, not n results per file, the grep -m 2 is per file max occurrence.
  2. I often use git grep which doesn't take -m

A good alternative in these scenarios is grep | sed 2q to grep first 2 occurrences across all files. sed documentation: https://www.gnu.org/software/sed/manual/sed.html

Share:
182,199

Related videos on Youtube

Jas
Author by

Jas

Updated on June 13, 2020

Comments

  • Jas
    Jas about 4 years

    I would like to say 10 lines max from grep.

    I don't want my computer to work hard. I want it to stop after 10 results found by grep. Is it possible?

  • Jas
    Jas over 13 years
    hi it tried it it basically works but it does not seem like the grep "stops" thinking after he finds the first 10 lines it looks like he continues thinking and "using my cpu" and just not printint it is it correcT? thansk
  • Attila O.
    Attila O. almost 13 years
    Note that you cannot use the | head pipe when using grep with -A or -B (and thus not only searching for result (-o), but for context as well). In that case you're left with -m to tell grep the number of lines with results to be returned.
  • Maic López Sáenz
    Maic López Sáenz about 12 years
    Using head does not actually stops grep from running through the whole file. Using the -m option in grep does.
  • Grégoire
    Grégoire about 11 years
    @Jason: this doesn't seem to be the case: grep takes 0.005s with -m 1 and 1.579s without on a file with 10 millions lines on my laptop.
  • Ar5hv1r
    Ar5hv1r over 10 years
    Piping into tail is generally going to work, but breaks down particularly if you're grepping with context, e.g. grep -A10 PATTERN, using tail truncates the context, rather than the number of results. This answer was what I was looking for.
  • Julien
    Julien over 8 years
    -m 10 is the option that makes the difference when grepping multiple files ! Piping to head won't show matches in subsequent files if there are too many matches in the first file. Thanks !
  • ishahak
    ishahak over 8 years
    IMHO this should be marked as the accepted answer, as it does not require another tool. BTW it is easier to remember this option when knowing that it is the shortcut of --max-count
  • Nikolaos Kakouros
    Nikolaos Kakouros over 6 years
    -m conflicts with -A/-B/-C options in old versions of grep, eg 2.25that can be found on Ubuntu 16.04, outputing only m lines instead of what is defined by -A/-B/-C. Newer versions do not have this issue (tested with 3.1 on Arch).