grep is your friend

grep is one of the most commonly-used UNIX utilities; it simply prints each line of input that matches a pattern specification called a regular expression. In combination with other utilities, it has a huge number of uses in searching and data manipulation. The name grep is derived from a commonly-used command in an early version of the UNIX line editor ed, which had a command of the form g/re/p, which meant to print each line of the input file that matched the given regular expression represented by "re". This was used so much that a separate command-line utility was written to perform this function.

Regular expressions are a way of specifying certain kinds of text patterns; the notation is cryptic (and based on an earlier mathematical notation) but powerful.

Basic regular expression elements:

. (a single period)
Match any single character.
[abc]
Match a single character a or b or c (an arbitrary list of characters can be specified in brackets).
[^abc]
Match any single character that is not a or b or c.
[a-z]
Shorthand to matcha single character in the contiguous range of characters a through z (this would match any lowercase alphabetic character). Similarly one can write multiple ranges like [A-Za-z], [0-9a-z], etc.
x (if not a special character)
Matches that single literal character.
\x
Matches the single character even if it has a special meaning in regular expressions, i.e. \*.
^
Matches the beginning of a line, i.e. ^# matches only lines that begin with #.
$
Matches the end of a line.
*
Matches zero or more occurrences of the previous match.

Next ->


Steve VanDevender
Last modified: Thu Jul 26 14:46:03 PDT 2007