Reputation: 11
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.
Upvotes: 1
Views: 90
Reputation: 133528
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.
Upvotes: 2
Reputation: 189407
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.
Upvotes: 3
Reputation: 103864
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
Upvotes: 1