![sed - 如何替换空格或下划线_word](https://linux22.com/image/3807/sed%20-%20%E5%A6%82%E4%BD%95%E6%9B%BF%E6%8D%A2%E7%A9%BA%E6%A0%BC%E6%88%96%E4%B8%8B%E5%88%92%E7%BA%BF_word.png)
目前我有这个 sed:
sed -i '/[^}.to]\.to[[:space:]]/ s/\(\S\)/expect(\1/' ../_spec_seded/"$file"
在有“ ”(但不是“ ”)expect(
的行的开头添加“ ”.to
{.to
我怎样才能使它匹配带有“ .to
”和“ .to_not
”的行?
我试过
sed -i '/[^}.to]\.to([[:space:]]|_not)/ s/\(\S\)/expect(\1/' my_file
基本上改为[[:space:]]
尝试([[:space:]]|_not)
匹配.to
OR.to_not
但这把两者都弄乱了,而不是在开始时添加期望。
答案1
这是你想要的吗?
sed '/{\.to/b;/\.to\(_not\|\)\s/s/^\s*/\0expect(/' file
替代:
sed '/{\.to/b;/\.to\(_not\|\)[[:space:]]/s/^[[:space:]]*/\0expect(/' file
样本:
$ diff -ytW 48 infile <(./sed_script)
.to do | expect(.to do
.to SPACE and | expect(.to SPACE and
.to TAB | expect(.to TAB
some s some s
day x day x
.to r | expect(.to r
.tofry .tofry
not t not t
.to tab | expect(.to tab
{.to not me {.to not me
{.to nor me {.to nor me
when e when e
{.to Nope {.to Nope
.to_not BUT yes! | expect(.to_not BUT yes
{.to Oh nono {.to Oh nono
.to_notX .to_notX
.do s .do s
{.to not do me {.to not do me
so d so d
|--- Original file ----|--- modified file ----|