251

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
Jas
  • 14,493
  • 27
  • 97
  • 148
  • 1
    In your case you don't want the computer to work hard.. But if it's just a human readability problem, you can use `less` via a pipe. That will fill the screen and you can hit ENTER to see more lines and `q` to quit: `grep "SomeText" somefile.csv | less` – SilentSteel Dec 22 '14 at 17:49

6 Answers6

332

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!

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Erik Pilz
  • 3,836
  • 2
  • 17
  • 7
  • 3
    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 – Jas Feb 16 '11 at 06:36
  • 6
    @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. – Grégoire Apr 17 '13 at 12:59
  • 3
    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. – dimo414 Oct 18 '13 at 16:39
  • 1
    `-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 ! – Julien Oct 08 '15 at 10:07
  • 1
    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 – ishahak Nov 25 '15 at 08:52
  • 2
    `-m` conflicts with `-A/-B/-C` options in old versions of grep, eg `2.25`that 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). – Nikolaos Kakouros Nov 24 '17 at 09:22
  • @Jas: note that this option tells `grep` to stop after *matching* 10 lines, not after *reading* 10 lines. Depending on your input, this may explain the behaviour you're experiencing. – Anthony Labarre Nov 22 '22 at 09:07
72

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.

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • 3
    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. – Attila O. Aug 30 '11 at 15:20
  • 22
    Using head does not actually stops grep from running through the whole file. Using the -m option in grep does. – Maic López Sáenz Apr 09 '12 at 06:29
7

Awk approach:

awk '/pattern/{print; count++; if (count==10) exit}' file
peterh
  • 11,875
  • 18
  • 85
  • 108
kurumi
  • 25,121
  • 5
  • 44
  • 52
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

Emily
  • 306
  • 4
  • 8
  • "the grep -m 2 is per file max occurrence." this seems to depend on grep version. I wanted this behavior and wasn't getting it (with "grep (BSD grep) 2.5.1-FreeBSD" on macos). I installed grep with homebrew on macos (brew install grep && ggrep foo) and that version ("ggrep (GNU grep) 3.7") behaves this way. – David Winiecki Apr 28 '22 at 17:18
1

Emily's answer (mid-2020) mentions:

I often use git grep which doesn't take -m.

Actually, it does (mid-2022):
With Git 2.38 (Q3 2022), "git grep -m<max-hits>"(man) is a way to limit the hits shown per file.

That means git grep -m can be used as an alternative to grep when done in a Git repository.

See commit 68437ed (22 Jun 2022) by Carlos López (00xc).
(Merged by Junio C Hamano -- gitster -- in commit 8c4f65e, 13 Jul 2022)

grep: add --max-count command line option

Signed-off-by: Carlos López 00xc@protonmail.com

This patch adds a command line option analogous to that of GNU grep(1)'s -m / --max-count, which users might already be used to.

This makes it possible to limit the amount of matches shown in the output while keeping the functionality of other options such as -C (show code context) or -p (show containing function), which would be difficult to do with a shell pipeline (e.g. head(1)).

git grep now includes in its man page:

-m <num>

--max-count <num>

Limit the amount of matches per file.

When using the -v or --invert-match option, the search stops after the specified number of non-matches.

  • A value of -1 will return unlimited results (the default).
  • A value of 0 will exit immediately with a non-zero status.
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

Using tail:

#dmesg 
...
...
...
[132059.017752] cfg80211:   (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
[132116.566238] cfg80211: Calling CRDA to update world regulatory domain
[132116.568939] cfg80211: World regulatory domain updated:
[132116.568942] cfg80211:   (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
[132116.568944] cfg80211:   (2402000 KHz - 2472000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
[132116.568945] cfg80211:   (2457000 KHz - 2482000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
[132116.568947] cfg80211:   (2474000 KHz - 2494000 KHz @ 20000 KHz), (300 mBi, 2000 mBm)
[132116.568948] cfg80211:   (5170000 KHz - 5250000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
[132116.568949] cfg80211:   (5735000 KHz - 5835000 KHz @ 40000 KHz), (300 mBi, 2000 mBm)
[132120.288218] cfg80211: Calling CRDA for country: GB
[132120.291143] cfg80211: Regulatory domain changed to country: GB
[132120.291146] cfg80211:   (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
[132120.291148] cfg80211:   (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291150] cfg80211:   (5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291152] cfg80211:   (5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291153] cfg80211:   (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm)
[132120.291155] cfg80211:   (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | head 2
head: cannot open ‘2’ for reading: No such file or directory
alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | tail -2
[132120.291153] cfg80211:   (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm)
[132120.291155] cfg80211:   (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | tail -5
[132120.291148] cfg80211:   (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291150] cfg80211:   (5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291152] cfg80211:   (5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291153] cfg80211:   (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm)
[132120.291155] cfg80211:   (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
alex@ubuntu:~/bugs/navencrypt/dev-tools$ dmesg | grep cfg8021 | tail -6
[132120.291146] cfg80211:   (start_freq - end_freq @ bandwidth), (max_antenna_gain, max_eirp)
[132120.291148] cfg80211:   (2402000 KHz - 2482000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291150] cfg80211:   (5170000 KHz - 5250000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291152] cfg80211:   (5250000 KHz - 5330000 KHz @ 40000 KHz), (N/A, 2000 mBm)
[132120.291153] cfg80211:   (5490000 KHz - 5710000 KHz @ 40000 KHz), (N/A, 2700 mBm)
[132120.291155] cfg80211:   (57240000 KHz - 65880000 KHz @ 2160000 KHz), (N/A, 4000 mBm)
alex@ubuntu:~/bugs/navencrypt/dev-tools$ 
peterh
  • 11,875
  • 18
  • 85
  • 108