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 searchsqs
string in lines that have 80 or less characters in them."\033[1;31m" $0 "\033[0m "
is used to color the linered
.
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 typeTYPE
.
[...] 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
Finding Executable Files Using Ls and Grep
Tcp Handshake with Sock_Raw Socket
How to Force Linking with a Static Library When a Shared Library of Same Name Is Present
Changing Default Shell in Linux
What Is the Meaning of Question Marks '' in Linux Kernel Panic Call Traces
Ffmpeg Split Avi into Frames with Known Frame Rate
How to Use Grep to Match But Without Printing the Matches
Create a Static Haskell Linux Executable
How to Export Database Schema in Oracle to a Dump File
How to Purge Disk I/O Caches on Linux
Recursively Find All Files Newer Than a Given Time
List of Possible Internal Socket Statuses from /Proc
Rsync - Mkstemp Failed: Permission Denied (13)
Getting Sudo and Nohup to Work Together
Ldconfig Error:"Is Not a Symbolic Link" When Using Linux Loader