Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed "undefined label" on MacOS

Tags:

macos

sed

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?

like image 461
ralfoide Avatar asked Sep 04 '12 22:09

ralfoide


People also ask

Can you use sed on Mac?

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.

What is sed Macos?

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.


1 Answers

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

like image 146
dopexxx Avatar answered Nov 15 '22 14:11

dopexxx