egrep: the extended global regular expression print in Unix allows to search for a string or complex patterns in a file. Regular expressions provide a convenient, compact way of expressing patterns.
Regular expressions in egrep are written slightly dofferently. For example, Kleen closure operator(*) is written as * (not superscript), union (È) is denoted by verticle bar (\mid). Parentheses have their usual meaning as grouping operators. The alphabet (å) is denoted by a period (.), i.e., matches any character.
For example, the regular expression (abÈc)* would be denoted as (ab \mid c)*, and å* would be denoted as .*
There is no way to express the empty string
in egrep. Instead, r? denotes zero or more occurences
of the regular expression r, so the empty string is
not needed in practice.
Special characters beginning of line
is denoted by ^
and end of line is denoted by
$
Expression | egrep notation |
r* | r* |
r+ | r+ |
r Èl | r? |
r Ès | r\mid s |
rs | rs |
(r) | (r) |
å | . |
%egrep 'depend' /usr/dict/words
searches the dictionary for all words containing depend as substring.
%egrep '^y.*y$' /usr/dict/words
In contrast, the command
%egrep 'y.*y' /usr/dict/words
results in a list of 113 words
%egrep '^rec(ei|ie)ve$' /usr/dict/words
%egrep '^s..u.t..e$' /usr/dict/words