简体   繁体   English

sed命令在solaris中无法正常工作但在linux中工作

[英]sed command not working properly in solaris but working in linux

I have following string which is used further in sed command. 我有以下字符串,在sed命令中进一步使用。 Its working properly in Linux but NOT working in Solaris 它在Linux中正常工作但不在Solaris中工作

-bash-3.00$ string="CREATESETTABLEDATABASE1.TABLE1(uid)CREATESETTABLEDATABASE1.TABLENAMEuid,cid,mid)DATABASE2.TABLENAME(hi,hello)" -bash-3.00 $ string =“CREATESETTABLEDATABASE1.TABLE1(uid)CREATESETTABLEDATABASE1.TABLENAMEuid,cid,mid)DATABASE2.TABLENAME(嗨,你好)”

In Linux box, it outputs properly as below. 在Linux框中,它输出正确如下。

echo $string | sed -e 's/.*CREATESETTABLE[^)]\+TABLENAME\(.*\)/\1/g'
uid,cid,mid)DATABASE2.TABLENAME(hi,hello)

I solaris , sed search is not working returns full string irrespective of search string match. 我的solaris ,sed搜索不工作返回完整的字符串,无论搜索字符串匹配。

echo $string | sed -e 's/.*CREATESETTABLE[^)]\+TABLENAME\(.*\)/\1/g'
CREATESETTABLEDATABASE1.TABLE1(uid)CREATESETTABLEDATABASE1.TABLENAMEuid,cid,mid)DATABASE2.TABLENAME(hi,hello)

I want the same output to be printed in solaris. 我希望在solaris中打印相同的输出。

I believe \\+ doesn't work on older sed even on BSD it is not supported. 我相信\\+即使在BSD上也不适用于旧的sed,它不受支持。 Try this sed: 试试这个sed:

sed -e 's/.*CREATESETTABLE[^)]*TABLENAME\(.*\)/\1/g'

POSIX sed supports only BRE (basic regular expressions) in which + has no special meaning . POSIX sed仅支持BRE (基本正则表达式),其中+ 没有特殊含义

One important oddity (relatively speaking) about BREs is that () and {} require \\ -escaping in order to gain their special meaning. 关于BRE的一个重要的奇怪之处(相对而言)是(){} 需要 \\ -escaping以获得它们的特殊含义。 Those characters, and only those characters, require such escaping. 那些字符, 只有那些字符,需要这样的转义。 The opposite is required in contemporary (ERE) expressions, \\ -escaping them is required to disable their special meaning. 在当代(ERE)表达式中需要相反的情况, \\取消它们需要禁用它们的特殊含义。

The behaviour of an escaped ( \\ ) non-special character in a BRE is undefined by the specification. 规范未定义 BRE中转义( \\ )非特殊字符的行为。

You problem stems from the fact that \\+ (along with \\? , and \\| within \\(\\) ) are GNU extensions . 你的问题源于这样的事实: \\+ (和\\?\\|\\(\\) )是GNU扩展

These BRE extensions preserve the convention of a \\ prefix, but when GNU sed is given the option -r it will enable ERE (extended regular expressions) in which + has its modern meaning (equivalent to {1,} ) and the requirement for the extra \\ is removed. 这些BRE扩展保留了\\前缀的约定,但是当GNU sed给出选项-r ,它将启用ERE(扩展正则表达式),其中+具有其现代意义(相当于{1,} )以及对它的要求额外\\被删除。 Similarly, standard BREs have no special meaning for ? 同样,标准BRE没有特殊含义? (or \\? , equivalent to {0,1} ), this feature is also enabled with -r . (或\\?相当于{0,1} ),此功能也使用-r启用。

If you use the GNU sed --posix option this will disable the various GNU extensions, and your scripts should in general be more portable (though perhaps more convoluted). 如果您使用GNU sed --posix选项,这将禁用各种GNU扩展,并且您的脚本通常应该更加可移植(尽管可能更复杂)。 Well nearly, prior to GNU sed 4.2 (April 2009) the --posix option did not disable all the BRE extensions, you should make sure to use an up to date version so that non-POSIX features don't creep in. 好吧,在GNU sed 4.2(2009年4月)之前--posix选项没有禁用所有BRE扩展,你应该确保使用最新版本,以便非POSIX功能不会进入。

The most portable way to achieve what you want is with {1,} : 实现您想要的最便携的方式是使用{1,}

echo $string | sed --posix -e 's/.*CREATESETTABLE[^)]\{1,\}TABLENAME\(.*\)/\1/g'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM