macOS

GNU Sed on the Mac

Like a lot of systems folks I use sed, a lot. It is probably an unhealthy amount of sed use but so be it. I operate on a lot of Linux servers but my desktop environment of choice is Mac OS X which means BSD tools (not the Linux-y GNU tools).

The differences are relatively subtle between BSD sed and GNU sed. However, most of my work is done through Terminal. I frequently find myself diving down rabbit holes and I really do not want to have to remember the differences between BSD and GNU sed.

Of course if you have homebrew you can install gnu-sed from it. This is more like a simple, single action solution. Anyway if you want only the Gnu-sed without brew you can compile it on Mac OS X:

It does not appear obvious to me that any internal OS X processes depend on BSD sed. The one annoyance I need to note when fully replacing the BSD sed version by following these steps is that after every OS update (10.11.4 to 10.11.5, etc.) you will have to run through all these steps again.

For me, it is all about consistently having the same sed everywhere. Most people either do not notice the difference or just deal with them. I choose not to and you are free to choose differently too.

  1. Disable Mac OS X System Integrity Protection (if you are going to have GNU sed as your default sed this is a requirement)

  2. Install Xcode (via Mac App Store)

  3. Install Xcode Command Line Tools:

    xcode-select --install
    
  4. Download the latest version of GNU sed from http://ftp.gnu.org/gnu/sed/

  5. Untar the sed package you just downloaded

  6. Open Terminal and cd into the unpacked GNU sed directory and configure the package:

    ./configure --prefix=/usr/ --program-suffix=-gnu
    
  7. Compile Gnu-sed:

    make -j 16
    
  8. Install it:

    sudo make install
    
  9. Here is why disabling SIP is needed. Move the system's sed to another file:

    mv /usr/bin/sed /usr/bin/sed-bsd
    
  10. Now create a link from gnu-sed to the default sed path:

    ln -s /usr/bin/sed-gnu /usr/bin/sed
    

If by any chance you need the regular system's sed (BSD sed) simply call it from it's new name bsd-sed or recreate the link pointing to it:

    ln -sf /usr/bin/sed-bsd /usr/bin/sed

 

© 2019 Mteam7 - All rights lorem ipsum dolor sit amet, consectetur adipiscing elit.*