← View all entries

How to work sed in macOS

Wanna use sed in macOS? Save yourself a headache and use the classic gnu-sed instead of the default sed that comes pre-installed with macOS! Trust me...

If you're familiar with the Linux version of sed, then just DON'T try the macOS sed version… 😅 I found out the hard way that their version of sed is filled with all these kinds of little differences and wrinkles compared to the classic sed I got used to in Ubuntu. Save yourself a headache and use the classic one instead:

# install gnu-sed
brew install gnu-sed

# enjoy
gsed "s/old/new/" test.txt

gnu-sed is a port of the sed command you'll find in Linux distros (vs the BSD version which is what you'll find in macOS). It will allow you to run the command gsed with all the basics you'd expect like more intuitive commands and support for escaped characters on the new string. You can take this a step further by replacing altogether your sed command for the gnu one. Though I rather keep them as they are just in case.

I know, I know… "Dude… it can't be too bad. Why not take the chance to learn it and up your game?" I told myself that and I spent a few hours learning about it and finding workarounds and even after I thought I was comfortable with it… I still found hang ups in what seemed to be very ordinary situations. My morning was gone just like that *snaps fingers* and I wasn't sure I was any closer to the solution. A few minutes after installing gsed I completed what I was trying to achieve all morning 🤦‍♂.

Anyway, if you do conquer BSD sed… Respect! ✊