Sed 在多行上替换从 x 到 y 的内容
Sed replace content from x till y on multi lines
我有以下文件:
2020-04-17 10:35:08.339 msw_im.c wync_ua[0]DEBUG: .mark1: lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren
2020-04-17 10:35:08.340
我想像这样替换 "mark1:" 和“2020-04-17 10:35:08.340”之间的每个字符
2020-04-17 10:35:08.339 msw_im.c wync_ua[0]DEBUG: .mark1: xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxx
xxxxxxxx
xxxxxxx
2020-04-17 10:35:08.340
我该怎么做?我试过:
$: sed -i '/mark1/,/^$/{s/./x/g}' file
有效,但也用 "x" 替换了第一行的开头。我尝试了其他多项 w/o 成功。有什么想法吗?
使用 GNU sed 匿名行:
sed -E -i '/mark1:/,/^$/{ /mark1:/{ :a;s/(mark1:x*)[^x]/x/;ta;b }; s/./x/g }' file
输出到文件:
2020-04-17 10:35:08.339 msw_im.c wync_ua[0]DEBUG: .mark1:xxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2020-04-17 10:35:08.340
参见:man sed
和 The Stack Overflow Regular Expressions FAQ
我有以下文件:
2020-04-17 10:35:08.339 msw_im.c wync_ua[0]DEBUG: .mark1: lorem ipsum
dolor sit amet, consetetur sadipscing elitr, sed diam
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren
2020-04-17 10:35:08.340
我想像这样替换 "mark1:" 和“2020-04-17 10:35:08.340”之间的每个字符
2020-04-17 10:35:08.339 msw_im.c wync_ua[0]DEBUG: .mark1: xxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx
xxxxx
xxxxxxxx
xxxxxxx
2020-04-17 10:35:08.340
我该怎么做?我试过:
$: sed -i '/mark1/,/^$/{s/./x/g}' file
有效,但也用 "x" 替换了第一行的开头。我尝试了其他多项 w/o 成功。有什么想法吗?
使用 GNU sed 匿名行:
sed -E -i '/mark1:/,/^$/{ /mark1:/{ :a;s/(mark1:x*)[^x]/x/;ta;b }; s/./x/g }' file
输出到文件:
2020-04-17 10:35:08.339 msw_im.c wync_ua[0]DEBUG: .mark1:xxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2020-04-17 10:35:08.340
参见:man sed
和 The Stack Overflow Regular Expressions FAQ