Tuesday, February 19, 2019

Replacing text using sed on OSX

Yet another reminder post for my self.

One again is bumped into the error message

sed: 1: "test.txt": undefined label 'est.txt'

when trying to replace text in a file using sed. When using sed on OSX to replace text in files, using -i, the BSD variant of sed on OSX requires an extension parameter, where a copy of the original file saved.

The syntax is as following

sed -i '.bak' 's/before/after' file.txt

If you are sure of what you are doing you can leave the extension parameter empty and not having a backup.

sed -i '' 's/before/after' file.txt

As a reference, here is the syntax for the GNU variant, ie. Ubuntu and friends

sed -i 's/before/after' file.txt