azalea says

OS X sed does not recognize \t as TAB character

Say you would like to replace multiple spaces with a tab (\t) in file with:

sed -i 's/ \+ /\t/g' file

ref

On Linux it works fine. However, on Mac OS X, sed will not give what you want.

The reason is that Mac OS X sed does not understand “\t”. You have to literally enter the tab by pressing Ctrl + v followed by Tab.

Also you need to explicitly give an empty string as the extension to edit in place.

Moreover, Mac OS X sed needs -E to recognize extended regular expressions. In this case, more than one spaces is the pattern ` + . Note that you do not have to escape +`.

With all the above changes, you’ll have to write:

sed -i '' -E 's/ + /     /g' file

ref

It is a pain to take care of so many differences between Mac OS X sed and the GNU sed. You can install GNU core utilities and leave all the differences out of your mind:

Install homebrew if you haven’t done so years ago already:

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

Then:

brew install coreutils findutils gnu-tar gnu-sed gawk gnutls gnu-indent gnu-getopt

This will install GNU find, sed, awk with a g-prefix. So now

gsed -i 's/ \+ /\t/g' file

will do exactly what you want.

If you absolutely need the GNU sed without the g-prefix, you can

brew install gnu-sed --default-names

ref


Bonus:

What if you want to run a script that calls sed on both Linux and OS X? You can do the following:

sed Mac OS X · Tweet Edit