Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

321
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda