Thursday, April 8, 2010

Change sed's Pattern Delimiter Character

sed has a neat feature as noted by mu where you can use any character to delimit the parts of the regular expression, which means you can control what characters would have to be escaped in your patterns.

Instead of using the normal forward-slash, you can use a pipe (|) or any character other than a backslash or newline character. Note that putting a backslash character before the delimiting character causes the character to be treated literally within the expression rather than as a delimiter.

For example, to replace all instances (because of the g) of the text "http://www.example.com/some/old/path" in old_file with "http://acme.com/some/new/path" and output to new_file, use:

sed -e 's|http://www.example.com/some/old/path|http://acme.com/some/new/path|g' old_file > new_file

No comments: