Modular Computers the manual GitHub

grep

grep: print lines that match a pattern.

Synopsis

grep [-ivnclqhHFE] pattern [file…]
grep [-ivnclqhHFE]
-e pattern [-e pattern]… [file…]

Description

grep reads each file and prints the lines that match any pattern. With no file, or where a file is -, it reads standard input. With more than one file, each line printed starts with its file’s name and a colon.

A pattern is a POSIX basic regular expression (BRE), or with -E an extended one (ERE), as far as Lua’s patterns can express them:

.
any character
*
the item before it, any number of times
^ and $
the start of the line (first in the pattern) and its end (last)
[…] and [^…]
a set of characters, or any character not in it; ranges such as a-z and the classes [:alpha:], [:digit:], [:alnum:], [:space:], [:upper:], [:lower:], [:punct:], [:xdigit:] and [:cntrl:] work inside it (with -i, [:upper:] and [:lower:] are any letter)
\c
the character c itself
+ and ?
with -E only: one or more, zero or one of the item before; in a BRE they are plain characters

Groups, alternation and intervals (\( \), \|, \{n,m\}, or with -E ( ) | { }) are not supported: such a pattern is refused with a message.

Options

-e pattern
Use pattern as a pattern; may be given more than once. Every argument that is not an option is then a file.
-E
Patterns are extended regular expressions.
-F
Patterns are fixed strings, matched as they are.
-i
Ignore case.
-v
Select the lines that do not match.
-n
Put each line’s number (and a colon) before it.
-c
Print only a count of the selected lines, for each file.
-l
Print only the names of the files with a selected line ((standard input) for standard input).
-q
Print nothing; exit 0 at the first selected line.
-h
Never put file names before lines.
-H
Always put file names before lines.
–
The arguments after it are all files.

Single-letter options may be combined, as in -in.

Exit status

0 if a line was selected, 1 if none was, 2 for an error (a bad option or pattern, or a file that could not be opened), unless -q found a line.

Examples

grep -n require /bin/ls
grep -ic "^local" *.lua
ps | grep -v sh
grep -E "ab+c?" notes.txt

Notes

There are no context options (-A, -B, -C), no -r and no -o.

See also

find(1), head(1), tail(1), sh(1)