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 \'
The name of the label terminates with the first literal newline, not at the semi-colon. There are two easy ways to solve the problem. Add literal newlines:
sed '/SUCCESSFUL/d
/\[java\]/!b label
s/\s\+\[java\]//
/^\s*$$/d; /Compiling/!d
:label
/^\s*$$/d
s/^/monitor: /'
Or use multiple -e
options:
sed -e '/SUCCESSFUL/d ; /\[java\]/!b label' \
-e 's/\s\+\[java\]//; /^\s*$$/d; /Compiling/!d' \
-e':label' -e'/^\s*$$/d; s/^/monitor: /'
There are a bunch of similar questions on SO but most of them are due to the behavior of -i
differing between platforms, so this is different.
In this case, the issue is rather simple: it seems like label references can only go backwards in the BSD version of sed
, whereas the GNU version allows to use forward references. That is on MacOS, the :label
must appear before the b label
.
The solution is to rewrite the expression to either define the label before the branch, or in the case of the expression above realize the branch is a kind of "if this pattern is not present ... jump ahead". In this case the expression can be expanded to not need the label in the first place:
sed '/SUCCESSFUL/d ; /\s+\[java\]\s*/d; /\[java\]/s/\s\+\[java\]//; /Compiling/!d; /^\s*$$/d; s/^/monitor: /'