加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

你可以在sed中使用哪些分隔符?

发布时间:2020-12-14 02:32:07 所属栏目:Linux 来源:网络整理
导读:我们通常会看到人们在想要使用包含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|
我们通常会看到人们在想要使用包含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 'saaabag' <<< "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.”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读