【发布时间】:2016-02-25 12:13:14
【问题描述】:
我需要使用sed 将 每个 匹配的两行模式替换为三行模式。这是我的输入文件 (tempfile.txt)。
lease 192.168.6.100 {
binding state free;
hardware ethernet 00:e0:4c:68:00:96;
}
lease 192.168.6.100 {
binding state active;
hardware ethernet 00:e0:4c:68:00:96;
client-hostname "andrew-H81M-S2PH";
}
lease 192.168.6.100 {
binding state free;
hardware ethernet 00:e0:4c:68:00:96;
}
lease 192.168.6.100 {
binding state active;
hardware ethernet 00:e0:4c:68:00:96;
client-hostname "andrew-H81M-S2PH";
}
基本上,如果client-hostname "HOSTNAME";
缺少,则应将其替换为 tab,然后是 newline。
我的尝试:
sed 'N; /hardware.*}/d; P; D' tempfile.txt
结果是:
lease 192.168.6.100 {
binding state free;
lease 192.168.6.100 {
binding state active;
hardware ethernet 00:e0:4c:68:00:96;
client-hostname "andrew-H81M-S2PH";
}
lease 192.168.6.100 {
binding state free;
lease 192.168.6.100 {
binding state active;
hardware ethernet 00:e0:4c:68:00:96;
client-hostname "andrew-H81M-S2PH";
}
这是我想要的输出。
lease 192.168.6.100 {
binding state free;
hardware ethernet 00:e0:4c:68:00:96;
<tab>
}
lease 192.168.6.100 {
binding state active;
hardware ethernet 00:e0:4c:68:00:96;
client-hostname "andrew-H81M-S2PH";
}
lease 192.168.6.100 {
binding state free;
hardware ethernet 00:e0:4c:68:00:96;
<tab>
}
lease 192.168.6.100 {
binding state active;
hardware ethernet 00:e0:4c:68:00:96;
client-hostname "andrew-H81M-S2PH";
}
如您所见,卷曲之间始终存在三行。这就是我的目标。
【问题讨论】:
-
我不太明白:“如果它丢失了,它应该被替换”......?你能显示所需的输出吗?
-
你能把这个添加到问题中吗?
-
我的意思是,edit 这个问题更清楚,对于未来的访问者也是如此,而无需深入研究 cmets。
-
完成。对此感到抱歉。
-
我已经编辑了这个问题:你实际上是用三行替换了两行匹配的行 - 我已经相应地更改了措辞。