SED 删除匹配行和后续行之前的行
SED Delete line Previous the Match and Proceeding Line
我有一个包含如下文本的文件:
Begin
Certificate
End
Begin
PKIValue1
End
Begin
PKIValue2
End
Begin
Certificate
End
我想删除与 Certificate
关联的三行,但为 PKIValues
保留剩余的 Begin
和 End
。
我一直在测试这个 SED 行:
sed -n -e '/Certificate/{x;1!p;g;$!N;p;D;}' -e h $cert_file>PKI.txt
那条线与我想要的相反。它只保留 Certificate
值和关联的 Begin
和 End
。如何删除 certificate
但保留 PKIValues
的其他行?
考虑:
$ sed -n -e '/Begin/{N;N;N;/Certificate/!p;}' file
Begin
PKIValue1
End
Begin
PKIValue2
End
工作原理
`/Begin/{N;N;N;/Certificate/!p;}`
每次有 Begin
行,我们将它和接下来的三行 N;N;N;
收集到模式 space 中。如果模式 space 现在包含 Certificate
,我们将跳过此分组。如果没有,我们打印它。
这个小gnu awk
应该做的:
awk '!/Certificate/' RS= ORS="\n\n" file
Begin
PKIValue1
End
Begin
PKIValue2
End
将 RS
记录选择器设置为空使其在块模式下工作(gnu 函数)
!/Certificate/'
这只是删除带有 Certificate
的方块
如果块中有不同数量的行,这会很好用。
我有一个包含如下文本的文件:
Begin
Certificate
End
Begin
PKIValue1
End
Begin
PKIValue2
End
Begin
Certificate
End
我想删除与 Certificate
关联的三行,但为 PKIValues
保留剩余的 Begin
和 End
。
我一直在测试这个 SED 行:
sed -n -e '/Certificate/{x;1!p;g;$!N;p;D;}' -e h $cert_file>PKI.txt
那条线与我想要的相反。它只保留 Certificate
值和关联的 Begin
和 End
。如何删除 certificate
但保留 PKIValues
的其他行?
考虑:
$ sed -n -e '/Begin/{N;N;N;/Certificate/!p;}' file
Begin
PKIValue1
End
Begin
PKIValue2
End
工作原理
`/Begin/{N;N;N;/Certificate/!p;}`
每次有 Begin
行,我们将它和接下来的三行 N;N;N;
收集到模式 space 中。如果模式 space 现在包含 Certificate
,我们将跳过此分组。如果没有,我们打印它。
这个小gnu awk
应该做的:
awk '!/Certificate/' RS= ORS="\n\n" file
Begin
PKIValue1
End
Begin
PKIValue2
End
将 RS
记录选择器设置为空使其在块模式下工作(gnu 函数)
!/Certificate/'
这只是删除带有 Certificate
如果块中有不同数量的行,这会很好用。