๐Ÿ“š General & Other

Replacement Strings Have Their Own Metacharacters

Most people know the search pattern is interpreted. Fewer realise the replacement is too.

Most people know the search pattern is interpreted. Fewer realise the replacement is too.

The ampersand expands

In sed, an unescaped & in the replacement is substituted with the entire matched text. Replacing a term with AT&T inserts the match where the ampersand was, producing output that looks like a bizarre corruption rather than an escaping error. Escape it as \&.

Backslashes and group references

\1 refers to a capture group. A replacement containing a literal backslash needs doubling, and a Windows path in a replacement string is almost entirely backslashes.

Delimiter collision

sed 's/old/new/' breaks when either side contains /, which URLs always do. Any character can be the delimiter โ€” s|old|new| โ€” and choosing one absent from both strings is the fix.

Order matters when chaining

Two replacements applied in sequence are not commutative. Replacing a with b and then b with c turns the original a into c. Where that is not intended, replace via a placeholder that cannot appear in the data.

Try it: Find and Replace Text on SeoWolf's Notepad.