如果下一行与模式匹配,我有删除下一行的问题。例如 :
define service{
host_name A
notification_period F
}
define service{
host_name A
notification_period metrologie
notification_period F
}
Run Code Online (Sandbox Code Playgroud)
我检查模式计量。如果下一行包含notification_period,我想删除行notification_period F。
我尝试使用命令 sed '/metrologie/{h;d;};/notification_period/{x;!d;}' $file
但它将字符串转换为:
define service{
host_name A
}
define service{
host_name A
notification_period metrologie
}
Run Code Online (Sandbox Code Playgroud)
我想要这个输出:
define service{
host_name A
notification_period F
}
define service{
host_name A
notification_period metrologie
}
Run Code Online (Sandbox Code Playgroud)
您可以使用
sed '/metrologie/{N;/\n.*notification_period/{s/\n.*//}}' file
Run Code Online (Sandbox Code Playgroud)
请参阅在线sed演示。
细节
/metrologie/- 找到一行,metrologie
然后
N; - 读取模式空间的下一行,在它之前有一个换行符/\n.*notification_period/ - 尝试将模式空间中的当前文本与模式匹配换行符、任何 0+ 个字符进行匹配, notification_period{s/\n.*//}} - 如果上述模式匹配,则用空字符串替换换行符及其后的所有内容。