sed: Regular Expressions Overview
1
1 5.1 Overview of regular expression in 'sed'
1 ===========================================
1
1 To know how to use 'sed', people should understand regular expressions
1 ("regexp" for short). A regular expression is a pattern that is matched
1 against a subject string from left to right. Most characters are
1 "ordinary": they stand for themselves in a pattern, and match the
1 corresponding characters. Regular expressions in 'sed' are specified
1 between two slashes.
1
1 The following command prints lines containing the word 'hello':
1
1 sed -n '/hello/p'
1
1 The above example is equivalent to this 'grep' command:
1
1 grep 'hello'
1
1 The power of regular expressions comes from the ability to include
1 alternatives and repetitions in the pattern. These are encoded in the
1 pattern by the use of "special characters", which do not stand for
1 themselves but instead are interpreted in some special way.
1
1 The character '^' (caret) in a regular expression matches the
1 beginning of the line. The character '.' (dot) matches any single
1 character. The following 'sed' command matches and prints lines which
1 start with the letter 'b', followed by any single character, followed by
1 the letter 'd':
1
1 $ printf "%s\n" abode bad bed bit bid byte body | sed -n '/^b.d/p'
1 bad
1 bed
1 bid
1 body
1
1 The following sections explain the meaning and usage of special
1 characters in regular expressions.
1