Name
grep — stdin stdout - file -- opt --help --version
Synopsis
grep [options
]pattern
[files
]
The grep
command is one of
the most consistently useful and powerful in the Terminal arsenal. Its
premise is simple: given one or more files, print all lines in those
files that match a particular regular expression pattern. For example,
if a file myfile contains these
lines:
The quick brown fox jumped over the lazy dogs! My very eager mother just served us nine pancakes. Film at eleven.
and we search for all lines containing “pancake,” we get:
➜ grep pancake myfile
My very eager mother just served us nine pancakes.
grep
also understands regular
expressions: special strings for matching text in a file. Here we
match lines ending in an exclamation point:
➜ grep '\!$' myfile
The quick brown fox jumped over the lazy dogs!
grep
can use two different
types of regular expressions, which it calls
basic and extended. They are
equally powerful, just different, and you may prefer one over the
other based on your experience with other grep
implementations. The basic syntax is in
Table 2-2.
Useful options
| Print only lines that do not match the regular expression. |
| Print only the names of files that contain matching lines, not the lines themselves. |
| Print only the names of files that do not contain matching lines. |
| Print only a count of matching lines. |
| In front of each line of matching output, print its original line number. |
| In front of each line of matching output, print the byte offset of the line ... |
Get Macintosh Terminal Pocket Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.