Documentation
¶
Overview ¶
Package grep implements the grep builtin command.
grep — print lines that match patterns
Usage: grep [OPTION]... PATTERN [FILE]...
grep [OPTION]... -e PATTERN [-e PATTERN]... [FILE]...
Search for PATTERN in each FILE. When FILE is -, read standard input. With no FILE, read standard input.
Accepted flags:
-E, --extended-regexp
Interpret PATTERN as an extended regular expression (ERE).
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings (not regexps),
separated by newlines, any of which is to be matched.
-G, --basic-regexp
Interpret PATTERN as a basic regular expression (BRE). This is
the default.
-i, --ignore-case
Ignore case distinctions in patterns and input data.
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
-c, --count
Suppress normal output; instead print a count of matching lines
for each input file.
-l, --files-with-matches
Suppress normal output; instead print the name of each input
file from which output would normally have been printed.
-L, --files-without-match
Suppress normal output; instead print the name of each input
file from which no output would normally have been printed.
-n, --line-number
Prefix each line of output with the 1-based line number within
its input file.
-H, --with-filename
Print the file name for each match. This is the default when
there is more than one file to search.
-h, --no-filename
Suppress the prefixing of file names on output.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
-q, --quiet, --silent
Quiet. Do not write anything to standard output. Exit with zero
status if any match is found, even if an error was detected.
-s, --no-messages
Suppress error messages about nonexistent or unreadable files.
-x, --line-regexp
Select only those matches that exactly match the whole line.
-w, --word-regexp
Select only those lines containing matches that form whole
words.
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern. If this option is used multiple
times, search for all patterns given.
-a, --text
Process a binary file as if it were text; all lines (including
those containing NUL bytes) are treated as text and may match.
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines.
-C NUM, --context=NUM
Print NUM lines of output context. Equivalent to -A NUM -B NUM.
Exit codes:
0 At least one match was found. 1 No matches were found. 2 An error occurred.
Memory safety:
All processing is streaming: input is read line-by-line with a per-line cap of MaxLineBytes (1 MiB). Lines exceeding this cap cause an error rather than an unbounded allocation. All read loops check ctx.Err() at each iteration to honour the shell's execution timeout and support graceful cancellation. Go's regexp package uses the RE2 engine which guarantees linear-time matching, preventing ReDoS attacks.
Index ¶
Constants ¶
const MaxContextBytes = 512 * 1024 // 512 KiB
MaxContextBytes is the aggregate byte cap applied per match group to both the before-context sliding window and the after-context output stream. A single match group may emit at most this many bytes of context lines (before and after counted separately). The global executor output limit acts as the ceiling across all groups combined.
const MaxContextLines = 1_000 // 1k lines
MaxContextLines caps -A/-B/-C to prevent excessive memory use.
const MaxLineBytes = 1 << 20 // 1 MiB
MaxLineBytes is the per-line buffer cap for the line scanner. Lines longer than this are reported as an error instead of being buffered.
Variables ¶
var Cmd = builtins.Command{
Name: "grep",
Description: "print lines that match patterns",
MakeFlags: registerFlags,
}
Cmd is the grep builtin command descriptor.
Functions ¶
This section is empty.
Types ¶
This section is empty.