Si 2 o más líneas consecutivas contienen un patrón específico, elimine todas las líneas coincidentes y conserve solo la primera línea.
En el siguiente ejemplo, cuando 2 o más líneas consecutivas contienen "IO lógico", debemos eliminar todas las líneas coincidentes pero mantener la primera línea.
Archivo de entrada:
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
Archivo de salida:
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
Respuesta aceptada:
Usando awk
:
awk '/logical IO/ {if (!seen) {print; seen=1}; next}; {print; seen=0}' file.txt
-
/logical IO/ {if (!seen) {print; seen=1}; next}
comprueba si la línea contienelogical IO
, si se encuentra y la variableseen
es falso, es decir, la línea anterior no contienelogical IO
, luego imprima la línea, configureseen=1
y vaya a la siguiente línea; de lo contrario, vaya a la siguiente línea ya que la línea anterior tienelogical IO
-
Para cualquier otra línea,
{print; seen=0}
, imprime la línea y los conjuntosseen=0
Ejemplo:
$ 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