How do I limit the number of results returned from grep?
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:
- I only want n overall results, not n results per file, the
grep -m 2
is per file max occurrence. - 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
Related videos on Youtube
Jas
Updated on June 13, 2020Comments
-
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 over 13 yearshi 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. almost 13 yearsNote that you cannot use the
| head
pipe when usinggrep
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 about 12 yearsUsing head does not actually stops grep from running through the whole file. Using the -m option in grep does.
-
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 over 10 yearsPiping into
tail
is generally going to work, but breaks down particularly if you're grepping with context, e.g.grep -A10 PATTERN
, usingtail
truncates the context, rather than the number of results. This answer was what I was looking for. -
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 over 8 yearsIMHO 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 over 6 years
-m
conflicts with-A/-B/-C
options in old versions of grep, eg2.25
that can be found on Ubuntu 16.04, outputing onlym
lines instead of what is defined by-A/-B/-C
. Newer versions do not have this issue (tested with3.1
on Arch).