1. Overview

The grep command is great for searching for patterns in one or more files. But sometimes we may want to limit the output to be more readable or if we only care about a certain number of results.

In this short tutorial, we’ll go through a few ways to limit the output of a grep search.

2. Limit grep Output Using –max-count

One way we can limit the number of results is by using the grep –max-count, or -m, option. With this option, grep stops reading a file after reaching the given number of matches.

Let’s set up a simple example. This script creates two test files populated with some test data:

#!/bin/bash

# make two files with some numbered lines of text
for file in test_one.log test_two.log
do
  rm -f $file
  for i in {0..11}
  do
    # even/odd variable to make output a little more interesting
    if [ $((i % 2)) -eq 0 ];
    then
      sign="even"
    else
      sign="odd"
    fi

    echo "line $i - time to grep - $sign" >> $file
  done
done

Let’s check the contents of one file:

$ cat test_one.log
line 0 - time to grep - even
line 1 - time to grep - odd
line 2 - time to grep - even
line 3 - time to grep - odd
line 4 - time to grep - even
line 5 - time to grep - odd
line 6 - time to grep - even
line 7 - time to grep - odd
line 8 - time to grep - even
line 9 - time to grep - odd
line 10 - time to grep - even
line 11 - time to grep - odd

Now, let’s run a grep command to limit the number of results to three for both files:

$ grep --max-count 3 even *.log

We see only three results from each file:

test_one.log:line 0 - time to grep - even
test_one.log:line 2 - time to grep - even
test_one.log:line 4 - time to grep - even
test_two.log:line 0 - time to grep - even
test_two.log:line 2 - time to grep - even
test_two.log:line 4 - time to grep - even

Let’s try a second command to explore a little more:

$ grep --max-count 8 even *.log

Since there are only six results in each file and our limit is eight, we see all results from all files:

test_one.log:line 0 - time to grep - even
test_one.log:line 2 - time to grep - even
test_one.log:line 4 - time to grep - even
test_one.log:line 6 - time to grep - even
test_one.log:line 8 - time to grep - even
test_one.log:line 10 - time to grep - even
test_two.log:line 0 - time to grep - even
test_two.log:line 2 - time to grep - even
test_two.log:line 4 - time to grep - even
test_two.log:line 6 - time to grep - even
test_two.log:line 8 - time to grep - even
test_two.log:line 10 - time to grep - even

For each file, the number of results never hits the limit.

Another benefit to using the –max-count option is grep stops reading the files after finding m matches, where m is our value for –max-count. Some other methods to limit grep output may need grep to process all the files passed in, which could cause more processing than necessary.

3. Limit grep Output Using head

A second way we can limit the output from grep is to pipe the results to head. This limits the output of grep to the number of lines given to the head command. The default is 10, but we can change it with the -n argument.

For example, let’s limit our grep results to three:

$ grep even *.log | head -n 3
test_one.log:line 0 - time to grep - even
test_one.log:line 2 - time to grep - even
test_one.log:line 4 - time to grep - even

One thing to note is that the once we reach the limit we gave to head, it sends a signal to grep to stop executing. So, like the –max-count option in grep, we save on unnecessary file processing.

One downside to this approach is that if we’re using context options in grep, like –before-context or –after-context, the context lines around the matching result will count toward the -n option limit. So we’ll have a limit on the output of grep, but it may not be a limit on the number of results. In that case, using the –max-count option with grep would be a better option.

4. Limit grep Output Using less

Another way we can limit output from grep is to pipe the results to less:

$ grep even *.log | less

This limits the output of grep to the terminal size and allows normal less navigation of the results.

5. Conclusion

In this short article, we looked at a few different ways to limit the output of grep. We looked at using the –max-count option in grep as well as piping grep results to head and less.

Comments are closed on this article!