I recently found out that this simple sed
expression work fine on Linux or under Cygwin but fails on Mac with an "undefined label" error:
$ sed '/SUCCESSFUL/d ; /\[java\]/!b label; s/\s\+\[java\]//; /^\s*$$/d; /Compiling/!d; :label /^\s*$$/d; s/^/monitor: /'
sed: 1: "/SUCCESSFUL/d ; /\[java ...": undefined label 'label; s/\s\+\[java\]//; /^\s*$$/d; /Compiling/!d; :label /^\s*$$/d; s/^/monitor: /'
sed
on MacOS is a BSD variant with different options than the GNU counterpart. However man sed
clearly indicates the MacOS version of sed
supports labels, so why this error, and most important how to solve it?
One way to make the GNU version of the SED to work on the Mac OS X, is to directly install the gnu-sed along with the default names which will assure you that you won't have to run different commands on both the operating systems. In order to install the gnu-sed on the Mac OS X, you can consider the steps shown below.
SED is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits, SED works by making only one pass over the input(s), and is consequently more efficient.
A quick fix for others with similar problems (on MacOS) might be:
prepend your string expression with an empty string: ''
For example: instead of
sed -i 's/foo/bar/g' text.txt
write:
sed -i '' 's/foo/bar/g' text.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With