sed“未定义标签";在MacOS上 [英] sed "undefined label" on MacOS

查看:86
本文介绍了sed“未定义标签";在MacOS上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现,这个简单的sed表达式在Linux或Cygwin下工作正常,但在Mac上却失败,并出现未定义标签"错误:

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

MacOS上的

sed是BSD变体,具有与GNU对应版本不同的选项.但是man sed清楚地表明MacOS版本的sed支持标签,那么为什么会出现此错误,最重要的是如何解决它?

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?

推荐答案

标签名称以第一个文字换行符结尾,而不以分号结尾.有两种简单的方法可以解决此问题.添加文字换行符:

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

或使用多个-e选项:

sed -e '/SUCCESSFUL/d ; /\[java\]/!b label' \
  -e 's/\s\+\[java\]//; /^\s*$$/d; /Compiling/!d' \
  -e':label' -e'/^\s*$$/d; s/^/monitor: /'

这篇关于sed“未定义标签";在MacOS上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆