Lesson 5
Replace Text with sed Substitution
Objective: Control substitution patterns, addresses, flags, and shell quoting.
sed 's/pattern/replacement/' petchipThe s instruction searches pattern space and replaces the first matching occurrence. The delimiter separates the pattern from the replacement. A line that does not match is normally printed unchanged. The pattern is a regular expression, not necessarily a literal string.
Constrain the match
sed 's/^Milo:/Mervin:/' petchipThe anchor and colon select the complete initial name token Milo in this dataset. The fifth record begins Mervin: in the output. This avoids accidentally changing a longer name beginning with Milo.
First occurrence or every occurrence
sed 's/:/#/' petchip
sed 's/:/#/g' petchipThe first command changes only the first colon on each line. The g flag changes every non-overlapping match on each processed line. Global does not mean “only once across the file”; sed applies the script during each input cycle.
Limit which lines are edited
sed '2,4s/:/#/g' petchip
sed '/^Sam /s/:cat:/:feline:/' petchipThe numeric range limits the first substitution to lines 2 through 4. The regex address limits the second to records starting with Sam followed by a space. The address selects lines; the substitution pattern selects text within those lines. These are two different decisions.
Alternate delimiters
printf '%s\n' '/srv/old/data' | sed 's|/srv/old/|/srv/new/|'Using | as the delimiter makes path substitutions easier to read. A delimiter appearing inside the pattern or replacement must be escaped appropriately. The replacement also has special syntax: & inserts the matched text, which the next lessons explain.
Shell variables require a separate decision
petname=Sam
sed "/^${petname} /s/:cat:/:feline:/" petchipDouble quotes allow the shell to expand this deliberately controlled variable. Single quotes would keep the dollar expression literal. Expansion does not make arbitrary data safe as a sed regex: metacharacters, delimiters, and replacement ampersands can change the script. Do not interpolate untrusted values into a command; use a tool and data interface suited to literal values.
The replacement text may contain special characters that affect the result of the substitution. The ampersand (&) represents the entire matched text, while backreferences such as \1, \2, and so on can be used to insert portions of the pattern captured by parentheses. Different delimiters may be chosen when the pattern or replacement contains many slash characters. For example, sed
s:/usr/local:/opt/tools:
file performs the same substitution while improving readability. Careful use of quoting is also important because the shell may interpret special characters before sed processes them. Single quotes are commonly used to protect regular expressions and replacement strings from shell expansion.
firstname=Mary
printf '%s\n' "Your name is $firstname"
printf '%s\n' 'Your name is $firstname'
The first printf expands Mary; the second prints the variable spelling literally. An unquoted phrase such as var1=The beautiful sea is parsed as an assignment followed by a command invocation, not necessarily a shell syntax error. Quote complete values containing spaces.