我有以下格式的数据:
#@ <id_wxyz_1>
A line written after this.
#@ <id_123>
A line written after this one also.
#@ <id_wxyz_2>
One more line.
#@ <id_yex_9>
Another line.
现在我要删除两行:在@中包含“wxyz”的行及其下一行。我想要的示例输出是:
#@ <id_123>
A line written after this one also.
#@ <id_yex_9>
Another line.
是否有一些linux命令也可以实现相同的功能,或者python中有一些有效的方法来实现相同的功能。我知道可以使用grep、sed等有选择地删除一行代码,但是可以使用linux命令有选择地删除两行代码吗?
编辑:给出的答案很好,但不适用于以下表格的输入:
#@ <id_wxyz_1>
A line written after this.
#@ <id_wxyz_2>
A line written after this.
#@ <id_wxyz_3>
A line written after this.
#@ <id_wxyz_4>
A line written after this.
#@ <id_wxyzadded5>
A line written after this.
对于上面的输入,我应该没有输出行。
再次编辑:另一组输入是:
#@ <id_wxyz0>
Line 1.
#@ <id_wxyz1>
line 2.
#@ <id_wxyz2>
line 3.
#@ <id_wxyz3>
line 4.
#@ <id_6>
line 5.
输出应该是
#@ <id_6>
line 5.
最佳答案
你可以用sed来做这件事。
/^#@ <.*wxyz.*>/ {
N #Add the next line to the pattern space
s/.*// #clear the line
N #Read another line
/^\n$/ d #if line was blank, delete and start next cycle (reading again)
D #Otherwise, delete up to newline, and start next cycle with that
}
注意:对于第二种情况,它实际上仍然输出一个空行
关于python - 删除2个连续的行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19618523/