我们通常会看到人们在想要使用包含sed分隔符的模式时抱怨sed中s’错误的未知选项.
例如,如果我们使用/:
$var="hel/lo" $sed "s/a/$var/g" <<< "haha" sed: -e expression #1,char 9: unknown option to `s'
所以我们建议使用另一个分隔符,例如|:
$sed "s|a|$var|g" <<< "haha" hhel/lohhel/lo
但是,我想知道sed可以接受的分界符是什么…因为它似乎几乎是任何字符,包括正则表达式(*,?,.,…)!
在我的sed(GNU sed)4.2.2中:
$sed 's/a/b/g' <<< "haha" hbhb $sed 's_a_b_g' <<< "haha" hbhb $sed 's#a#b#g' <<< "haha" hbhb $sed 's$a$b$g' <<< "haha" hbhb $sed 's?a?b?g' <<< "haha" hbhb $sed 's*a*b*g' <<< "haha" hbhb $sed 's-a-b-g' <<< "haha" hbhb $sed 's.a.b.g' <<< "haha" hbhb $sed 'sXaXbXg' <<< "haha" hbhb $sed 'sxaxbxg' <<< "haha" hbhb $sed 's1a1b1g' <<< "haha" hbhb
如果它被转义,即使是在这里工作:
$sed 'saaabag' <<< "haha" sed: -e expression #1,char 5: unknown option to `s' $sed 'sa\aabag' <<< "haha" hbhb
这有什么规格吗?
解决方法
也许最接近标准的POSIX / IEEE Open Group Base Specification
says:
[2addr] s/BRE/replacement/flags
Substitute the replacement string for instances of the BRE in the
pattern space. Any character other than backslash or newline can be used instead of a slash to delimit the BRE and the replacement. Within the BRE and the replacement,the BRE delimiter itself can be used as a literal character if it is preceded by a backslash.”
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。