仅保留与模式匹配的每个连续行序列中的第一行

eka*_*sis 5 sed awk text-processing

如果 2 个或更多连续行包含特定模式,则删除所有匹配的行并仅保留第一行。

在下面的示例中,当 2 个或多个连续行包含“逻辑 IO”时,我们需要删除所有匹配的行但保留第一行。

输入文件:

select * from test1 where 1=1
testing logical IO 24
select * from test2 where condition=4
parsing logical IO 45
testing logical IO 500
handling logical IO 49
select * from test5 where 1=1
testing logical IO 24
select * from test5 where condition=78
parsing logical IO 346
testing logical IO 12
Run Code Online (Sandbox Code Playgroud)

输出文件:

select * from test1 where 1=1
testing logical IO 24
select * from test2 where condition=4
parsing logical IO 45
select * from test5 where 1=1
testing logical IO 24
select * from test5 where condition=78
parsing logical IO 346
Run Code Online (Sandbox Code Playgroud)

hee*_*ayl 8

使用awk

awk '/logical IO/ {if (!seen) {print; seen=1}; next}; {print; seen=0}' file.txt 
Run Code Online (Sandbox Code Playgroud)
  • /logical IO/ {if (!seen) {print; seen=1}; next}检查该行是否包含logical IO,如果找到并且变量seen为假,即前一行不包含logical IO,然后打印该行,设置seen=1并转到下一行,否则转到下一行,就像上一行一样logical IO

  • 对于任何其他行,{print; seen=0}, 打印该行和集合seen=0

例子:

$ cat file.txt 
select * from test1 where 1=1
testing logical IO 24
select * from test2 where condition=4
parsing logical IO 45
testing logical IO 500
select * from test5 where 1=1
testing logical IO 24
select * from test5 where condition=78
parsing logical IO 346
parsing logical IO 346
testing logical IO 12

$ awk '/logical IO/ {if (!seen) {print; seen=1}; next}; {print; seen=0}' file.txt 
select * from test1 where 1=1
testing logical IO 24
select * from test2 where condition=4
parsing logical IO 45
select * from test5 where 1=1
testing logical IO 24
select * from test5 where condition=78
parsing logical IO 346
Run Code Online (Sandbox Code Playgroud)

  • 好的。我喜欢你避免代码高尔夫并使用人类可读的变量名称,`seen`。 (2认同)