OS X – Installing GNU-sed
OS X Technical
Installing GNU-sed using Home Brew repository
Last month’s technical article focused on adding BASH 4 to OS X using Home Brew, because OS X ships with BASH v3.257 due to GPLv3.0 licensing requirements. You can read the article here. https://www.catracing.org/hendrb/tech-tutorial-adding-bash-4-osx-using-home-brewos-x/. This month I will share some more tips that will make your shell experience more compatible with Linux scripts by adding GNUsed, First some background then we will fire up the terminal.app and get started!
Back in 2015, I published article containing a type in yourself BASH script that will display random quotes from a file called ‘quotations’ that I converted from a CNET Amiga BBS plus file (Door, or external for the PC BBS SysOps). (Click here for article.) While it ran great on Linux boxes, due to differences in the BSD-sed (stream editor) utility, which is used to parse the blocks in the text file containing the individual quotes then centering them depending on the width of your terminal window. Would work on a Linux box, but fail with an error on OS X.
(As you can see the script runs fine in CentOS Linux)
(The script failing in OS X with the ‘expected context address’)
After upgrading to BASH 4 using the Home Brew repository I discovered you could also install the GNU version of the sed utility which would allow me to run my script error free without writing 2 versions of my script. Which brings us to the purpose of this blog, installing GNU-sed from the Home Brew repository.
There are 2 way we can install this from the repository, one method will install the files renaming GNUsed to gsed. Which will allow you to either call ‘sed’ (BSDsed), or ‘gsed’ (GNUsed) as needed from your scripts.
Method 1 :
Enter the OS X Terminal (SHIFT – COMMAND – U) Then launch the terminal.app.
You must be in an account with elevated privledges (NOTE: You can NOT sudo)
If logged in a normal user account (Which you should be!), Type the following at the terminal prompt.
$ su – <username>
The – option tell the terminal to switch users and load the environment of that user.
At the Shell Prompt type
$ brew install gnu-sed –with-default-names
The Brew repository installer will install sed into /usr/local/Cellar/gnu-sed/4.2.2/, and create a symbolic link for /usr/bin/local/sed to that directory
Now when you use sed, it will use GNU-sed instead of BSD sed.
You can revert back to BSDsed by removing the symbolic link in /usr/local/bin.
$ rm sed
You also need to remove the symlink to the man pages /usr/local/share/man/man1
$ rm /usr/local/share/man/man1/sed.1
NOTE: This does not remove the files only the symbolic links!
To permanently remove GNU-sed from your computer, simply type the following with your elevated account.
$brew remove gnu-sed
Method 2: Installing GNU-sed so it can be installed concurrently with BSDsed (dogs and cats living together.)
Let’s say that we wanted to install GNU-sed but for whatever reason (Such as compatibility), you wanted to install GNU-sed and have it reside on the system concurrently with BSDsed! Or give your users the option to choose which version of SED they want to use by default. This is quite simple to accomplish.
Enter the OS X Terminal (SHIFT – COMMAND – U) Then launch the terminal.app.
You must be in an account with elevated privileges (NOTE: You can NOT sudo)
If logged in a normal user account (Which you should be!)
Type at the terminal prompt.
$ su – <username>
The – option tell the terminal to switch users and load the environment of that user.
At your elevated prompt type.
$ brew install gnu-sed
The brew repository will install gnu-sed in the same dictory as method 1, but it will not create symbolic links for sed, or the sed man pages. You now have BSD-sed ‘sed’, and GNU-sed ‘gsed’ on your system! Type it our, type $ man sed, and $ man gsed at a terminal prompt! Scroll down to the last line of the page and note the difference!
If you are writing scripts that require the use of GNU-sed for your own environment just remember to use gsed, if you need to run scripts written for Linux machines, you may want to consider writing a launching scrip that aliases sed to gsed and then removes the alias upon exit. This can be done like this.
#!/bin/bash
# OS X Launcher for randquote.sh
# You MUST have gnu-sed installed from homebrew to use
# aliases sed to gnu-sed for linux bash script compatibility
alias sed=’gsed ‘
source ./randquote.sh
unalias sed
(Executiing randquote.sh with the launching script above, note that I needed to execute the script using the source command, instead of launching it using ./filename.sh)
In order for aliases to work inside a shell script, you must execute the script using the source command, not with the simple .\ from the command prompt. Your other option is to use a text editor and replace all incidents of sed with gsed manually.
Unless there is a specific reason option one will not work for you, I highly recommend installing GNU-sed with the –with-default-names option. It will save extra steps and headaches down the road, and can always be easily removed if you suspect compatibility issues.
Comments
OS X – Installing GNU-sed — No Comments
HTML tags allowed in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>