如果字符串匹配,则删除行,下一行包含另一个字符串 [英] delete line if string is matched and the next line contains another string
问题描述
遇到了一个烦人的文本操作问题,如果文件中包含一个字符串,但仅当下一行也包含另一个字符串时,我才需要删除该行.例如,我有以下几行:
got an annoying text manipulation problem, i need to delete a line in a file if it contains a string but only if the next line also contains another string. for example, i have these lines:
john paul
george
john paul
12
john paul
如果要立即删除包含"john paul"的任何行,然后紧随其后的是包含"george"的行,则我将其删除:
i want to delete any line containing 'john paul' if it is immediately followed by a line that contains 'george', so it would return:
george
john paul
12
john paul
不确定如何grep或sed这个.如果有人可以伸出援助之手,那就太好了!
not sure how to grep or sed this. if anyone could lend a hand that'd be great!
推荐答案
这可能对您有用(GNU sed):
This might work for you (GNU sed):
sed '/john paul/{$!N;/\n.*george/!P;D}' file
如果该行包含 john paul
,请阅读下一行,如果包含 george
,则不要打印第一行.
If the line contains john paul
read the next line and if it contains george
don't print the first line.
如果包含 george
的行包含 john paul
,则也会对其进行检查.
N.B. If the line containing george
contains john paul
it will be checked also.
这篇关于如果字符串匹配,则删除行,下一行包含另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!