How to Limit the Number of Results Returned from Grep

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

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!

How to limit the number of characters returned by each match in grep

You may try this find + awk:

find . -type f -exec awk 'length() <= 80 && /sqs/ {
print FILENAME ":" FNR ":\033[1;31m" $0 "\033[0m "}' {} +
  • length() <= 80 will search sqs string in lines that have 80 or less characters in them.
  • "\033[1;31m" $0 "\033[0m " is used to color the line red.

grep - limit number of files read

Naming 100,000 filenames in your command args is going to cause a problem. It probably exceeds the size of a shell command-line.

But you don't have to name all the files if you use the recursive option with just the name of the directory the files are in (which is . if you want to search files in the current directory):

grep -l -r 'str1' . | head -1

grep limited characters - one line


 egrep -Rso '.{0,40}wp-content.{0,40}' *.sh

This will not call the Radio-Symphonie-Orchestra, but -o(nly matching).

A maximum of 40 characters before and behind your pattern. Note: *e*grep.

How to remove line limit from grep

I suspect you have binary data in your log file.
Once grep matches a line with binary data in it, grep prints Binary file (standard input) matches (to stdout, not stderr!) and exits. All matches after the binary part will be ignored.

To confirm this theory run

grep . debug-2020-09-14.log | grep -x 'Binary file .* matches'

If this is indeed the problem, then you can fix it using grep's -a option. Here we also replaced cat and wc -l by grep's capabilities.

grep -ac aaaa debug-2020-09-14.log

From man grep:

-a, --text

Process a binary file as if it were text;
this is equivalent to the --binary-files=text option.

--binary-files=TYPE

If a file's data or metadata indicate that the file contains binary data, assume that the file is of type TYPE.
[...] grep suppresses output after null input binary data is discovered [...]. When some output is suppressed, grep follows any output with a one-line message saying that a binary file matches.

limit number of results of find, head is not working

How about use xargs instead of exec?

Check it, works good.
find / -type f | head -n5 | tr '\n' '\0' | xargs -0 ls

Your case:
find -L $line | head -n50 | tr '\n' '\0' | xargs -0 ls > $a$(basename $line)

How to truncate long matching lines returned by grep or ack

You could use the grep option -o, possibly in combination with changing your pattern to ".{0,10}<original pattern>.{0,10}" in order to see some context around it:


-o, --only-matching
Show only the part of a matching line that matches PATTERN.

..or -c:


-c, --count
Suppress normal output; instead print a count of matching lines
for each input file. With the -v, --invert-match option (see
below), count non-matching lines.


Related Topics



Leave a reply



Submit