LinuxCommandLibrary

grap

Search files for specific patterns

TLDR

Process a grap file and save the output file for future processing with pic and groff

$ grap [path/to/input.grap] > [path/to/output.pic]
copy

Typeset a grap file to PDF using the [me] macro package, saving the output to a file
$ grap [path/to/input.grap] | pic -T [pdf] | groff -[me] -T [pdf] > [path/to/output.pdf]
copy

SYNOPSIS

grep [OPTIONS] PATTERN [FILE...]

PARAMETERS

-i, --ignore-case
    Ignores case distinctions in patterns and input.

-v, --invert-match
    Selects lines that do not match the pattern.

-r, --recursive
    Recursively searches files under each directory.

-l, --files-with-matches
    Prints only the names of files containing matches.

-n, --line-number
    Prefixes output lines with their 1-based line numbers.

-c, --count
    Prints a count of matching lines for each file.

-E, --extended-regexp
    Interprets pattern as an extended regular expression.

-F, --fixed-strings
    Interprets pattern as fixed strings, not regular expressions.

-A NUM, --after-context=NUM
    Prints NUM lines of context after a match.

-B NUM, --before-context=NUM
    Prints NUM lines of context before a match.

-C NUM, --context=NUM
    Prints NUM lines of context around a match.

-w, --word-regexp
    Matches only whole words.

-x, --line-regexp
    Matches only lines that exactly match the whole pattern.

-o, --only-matching
    Prints only the matched parts of a line, each on a new line.

--color[=WHEN]
    Highlights matched strings with color; WHEN can be 'never', 'always', or 'auto'.

DESCRIPTION

grep is a powerful command-line utility for searching plain-text data for lines matching a regular expression. Named after the ed editor command g/re/p (global / regular expression / print), it filters log files, searches source code, and processes text. grep can search recursively, count matches, invert results, show context, and use various regular expression types. It's an indispensable tool for administrators and developers, offering flexibility and precision in text manipulation.

CAVEATS

grep is case-sensitive by default. Performance may vary with very large files or complex regular expressions. Proper escaping of shell special characters in patterns is crucial to avoid unexpected behavior.

REGULAR EXPRESSIONS

grep uses regular expressions to define search patterns. By default, it supports Basic Regular Expressions (BRE). The -E option enables Extended Regular Expressions (ERE), offering more features like + (one or more), ? (zero or one), and | (alternation).

EXIT STATUS

grep exits with status 0 for matches, 1 for no matches, and 2 for errors. This exit code is vital for scripting and conditional execution.

HISTORY

grep was written by Ken Thompson in 1974 for Unix, named from the ed editor command g/re/p. Early variants like egrep (extended regex) and fgrep (fixed strings) existed, but their functionality is now integrated into modern grep via options like -E and -F. It remains a fundamental Unix-like utility, known for its enduring design.

SEE ALSO

sed(1), awk(1), find(1), cat(1), less(1), xargs(1)

Copied to clipboard