• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

239
Views
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 3 years ago · Santiago Trujillo
3 answers
Answer question

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 3 years ago · Santiago Trujillo Report

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 3 years ago · Santiago Trujillo Report

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 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error