如果下一行与sed匹配,如何删除下一行?

时间:2019-09-27 07:39:02

标签: sed

如果下一行与模式匹配,我将无法删除下一行。 例如:

<input type="checkbox" onclick="checkFluency()"  id="fluency" />

我检查图案计量。如果下一行包含notification_period,我要删除行notification_period F。

我尝试使用命令sed'/ metrologie / {h; d;}; / notification_period / {x;!d;}'$ file

但是它将字符串转换为:

define service{
host_name               A
notification_period     F
}


define service{
host_name               A
notification_period     metrologie
notification_period     F
}

我想要这个输出:

define service{
host_name               A

}


define service{
host_name               A
notification_period     metrologie
}

2 个答案:

答案 0 :(得分:4)

您可以使用

/metrologie/

请参见online sed demo

详细信息

  • metrologie-用N;查找行 接着
    • /\n.*notification_period/-读取模式空间的下一行,并在其前面加上换行符
    • notification_period-尝试将模式空间中的当前文本与模式匹配的换行符(任何0+个字符,{s/\n.*//}}
    • )进行匹配
    • friend-如果上述模式匹配,请用空字符串替换换行符及其后的所有字符。

答案 1 :(得分:0)

这可能对您有用(GNU sed):

sed ':a;/metrologie/{n;/notification_period/d;ba}' file

如果一行包含metrologie,请打印并获取下一行。如果该行包含notification_period,请将其删除,否则重复。