sed “undefined label” on MacOS

前端 未结 2 1578
温柔的废话
温柔的废话 2021-02-06 00:52

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 \'         


        
2条回答
  •  情歌与酒
    2021-02-06 01:44

    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: /'
    

提交回复
热议问题