| www.delorie.com/gnu/docs/sed/sed_5.html | search |
![]() Buy the book! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
To know how to use sed, people should understand regular
expressions (regexp for short). A regular expression
is a pattern that is matched against a
subject string from left to right. Most characters stand for
themselves in a pattern, and match the corresponding characters
in the subject. As a trivial example, the pattern
The quick brown fox |
matches a portion of a subject string that is identical to
itself. The power of regular expressions comes from the
ability to include alternatives and repetitions in the pattern.
These are encoded in the pattern by the use of metacharacters,
which do not stand for themselves but instead
are interpreted in some special way. Here is a brief description
of regular expression syntax as used in sed.
char
*
\+
\?
\{i\}
\{i,j\}
\{i,\}
\(regexp\)
\(abcd\)*:
this will search for zero or more whole sequences
of `abcd', while abcd* would search
for `abc' followed by zero or more occurrences
of `d'. Note that this is not in the POSIX
standard and hence is not portable.
.
^
^#include will match only
lines where `#include' is the first thing on line--if
there are spaces before, for example, the match fails.
$
^, but refers to end of line
[list]
[^list]
The caret reverses the meaning of the regexp, so that it matches any single character NOT in list. To include `]' in the list, make it the first character (after the caret if needed), to include `-' in the list, make it the first or last; to include `^' put it after the first character.
regexp1\|regexp2
\digit
\(...\) reference in the
regular expression.
\char
\n for a new-line; in particular
\t matches a `t' under most implementations
of sed, rather than a tabulation character.
Note that the regular expression matcher is greedy, i.e., if two or more matches are detected, it selects the longest; if there are two or more selected with the same size, it selects the first in text.
Examples:
A that is the last
character on line, with at least nine preceding characters.
A that is the 16th character on a line.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |