Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

317
Vistas
How to use SED to remove texts starting from the first pattern until the second occurrence of the end pattern?

I am trying to use sed to remove texts starting from the line with the first pattern until the line with the second occurrence of the end pattern.

For example, the first pattern is BEGIN, and the end pattern is BREAKPOINT.

Input Example:

qq
ww
BEGIN
ab
ef
BREAKPOINT
ij
mn
BREAKPOINT
kk
BREAKPOINT
zz
yy
xx
BEGIN
ab
ef
BREAKPOINT
ij
mn
BREAKPOINT
pp

Expected Output:

qq
ww
kk
BREAKPOINT
zz
yy
xx
pp

Could you please kindly tell me how, or maybe an alternative way to achieve this?

PS. I know only how to show texts between the first pattern until the second occurrence of the end pattern, but I do not know how to remove them.

sed -n '/BEGIN/{:a;N;s/BREAKPOINT/&/2p;Ta}' file

Thanks.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

It's almost certainly not impossible to do this in sed, but also almost certainly somewhere between hard and impossible to understand the solution some weeks from now unless you are extremely familiar with sed. Better then to switch to a more human-readable scripting language:

awk '/^BEGIN$/ && !s { s=2 }
!s
/^BREAKPOINT$/ && s { s-- }' file

In very brief, the variable s keeps track of whether we have seen the beginning separator and if so how many times we want to see the terminating separator before reverting to printing the input lines.

over 4 years ago · Santiago Trujillo Denunciar

0

With your shown samples only, please try following awk code, written and tested in GNU awk.

awk '
/^BEGIN/,/^BREAKPOINT/{
  flag=1
  next
}
flag && /BREAKPOINT/{
  flag=""
  next
}
!flag
' Input_file

Explanation: Adding detailed explanation for above.

awk '                     ##Starting awk program from here.
/^BEGIN/,/^BREAKPOINT/{   ##Checking range from line starts from BEGIN till line starts BREAKPOINT then do following.
  flag=1                  ##Setting flag to 1 here.
  next                    ##next will skip all further statements from here.
}
flag && /BREAKPOINT/{     ##Checking condition if flag is SET and BREAKPOINT is found then do following.
  flag=""                 ##Nullifying flag here.
  next                    ##next will skip all further statements from here.
}
!flag                     ##Checking condition if flag is NULL then print current line.
' Input_file              ##Mentioning Input_file name here.
over 4 years ago · Santiago Trujillo Denunciar

0

A perl:

perl -0777 -pe 's/^BEGIN[\s\S]*?^BREAKPOINT[\s\S]*?^BREAKPOINT\R//gm' file

Or,

perl -0777 -pe 's/^BEGIN(?:[\s\S]*?^BREAKPOINT\R){2}//gm' file
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda