我们通常会看到人们在想要使用包含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.”