在 sed 中使用分号作为命令分隔符有多便携? [英] How portable is it to use semi-colons as command separators in sed?

查看:114
本文介绍了在 sed 中使用分号作为命令分隔符有多便携?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 sed 中,使用分号分隔的多个命令是相当常见的:

<预>$ sed -e '/re/{s//replace/p;q;}

但是,标准(例如 http://pubs.opengroup.org/onlinepubs/9699919799/ ) 只允许换行符作为分隔符:

<预>$ sed -e '/re/{s//替换/pq}

sed 的许多常见实现是否仍在使用中不允许使用分号?IOW,一个旨在可移植的 sed 脚本可以使用分号吗?

解决方案

来自 sed 的 POSIX 规范:

<块引用>

{abci以外的命令动词>、rtw:# 可以跟在后面通过<分号>,可选的 字符和另一个命令动词.但是,当 s 命令动词与 w 标志一起使用时,以这种方式跟在它后面的另一个命令会产生未定义的结果.

所以除了上面提到的那些命令之外,大多数命令都可以用分号分隔.

/[^\{abcirtw:#];[[:space:]]*/ :-)

In sed, it is fairly common to use multiple commands separated by semi-colons:

$ sed -e '/re/{s//replace/p; q;}

However, the standard (eg http://pubs.opengroup.org/onlinepubs/9699919799/ ) only allows for newlines as a separator:

$ sed -e '/re/{
    s//replace/p
    q
}

Are there many common implementations of sed still in use that do not allow the semi-colon? IOW, can a sed script intended to be portable use semi-colons?

解决方案

From the POSIX specification of sed :

Command verbs other than {, a, b, c, i, r, t, w, :, and # can be followed by a <semicolon>, optional <blank> characters, and another command verb. However, when the s command verb is used with the w flag, following it with another command in this manner produces undefined results.

So most commands, besides those mentioned above, can be separated by a semicolon.

/[^\{abcirtw:#];[[:space:]]*/ :-)

这篇关于在 sed 中使用分号作为命令分隔符有多便携?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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