我 try catch 这些字符串块并使用正则表达式和 sed 对它们进行注释。
每个块用空格分隔
some text here
some text here
AppServer1:
name: ${AppServer1.name}
ip: ${AppServer1.ip}
some text here
some text here
AppServer2:
name: ${AppServer1.name}
ip: ${AppServer1.ip}
some text here
some text here
我尝试使用这个正则表达式:sed E '/./{H;1h;$!d} ; x ; s/[^\$AppServer1](AppServer1)/#\1/gi'
但结果是: #AppServer1:
name: $#{AppServer1.name}
ip: $#{AppServer1.ip}
我在这里缺少评论完整字符串的内容:#AppServer1:
# name: ${AppServer1.name}
# ip: ${AppServer1.ip}
最佳答案
使用 gnu-sed
, 你可以这样做:
sed '/^AppServer1/I{:a; /^[[:blank:]]*$/!{s/.*/#&/; n; ba;} }' file
some text here
some text here
#AppServer1:
# name: ${AppServer1.name}
# ip: ${AppServer1.ip}
some text here
some text here
AppServer2:
name: ${AppServer1.name}
ip: ${AppServer1.ip}
some text here
some text here
详情:/^AppServer1/I
: 搜索 AppServer1
不区分大小写 {
: 块启动:a
: 制作标签a
/^[[:blank:]]*$/!
如果一行不是空行 {s/.*/#&/; n; ba;}
:在每一行前面加上 #
, 阅读下一行并转到标签 a
}
: 座结束 使用
awk
你可以这样做:awk '/^AppServer1/ {b=1} b && !NF {b=0} b {$0 = "#" $0} 1' file
some text here
some text here
#AppServer1:
# name: ${AppServer1.name}
# ip: ${AppServer1.ip}
some text here
some text here
AppServer2:
name: ${AppServer1.name}
ip: ${AppServer1.ip}
some text here
some text here
关于正则表达式和 sed 在多行 block 中查找字符串 block ,直到它以空格结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68725975/