Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a multiline block of text between regex pattern using sed

Tags:

regex

bash

sed

awk

I have a duplicated block of text I need to delete in a large xml file. I want to keep the first block and delete the second all within the same xml tag. For example:

<!--#if--> 
 -- several lines of text
<!--#else-->
-- several lines of the same text
<!--#endif-->

I'd like to delete the second block between the else and endif, and keep the keep the block between the if and else tags. Any help much appreciated - the script ends up deleting the entire file.

sed -i '/^<!--#else-->/ {p; :a; N; /^\<\!--\#endif--\>/!ba; s/*.\n//}; d' test.xml
like image 912
user2167052 Avatar asked Sep 29 '22 02:09

user2167052


1 Answers

I think this should work for you

sed '/--#else--/,/--#endif--/{//!d}' test.xml

this will delete the lines between else and endif

if you want to delete else and endif as well use this:

sed '/--#else--/,/--#endif--/d' test.xml

in the case you mentioned in the comments try this:

sed -n '/--#else--/,/--#endif--/p' test.xml

-n is dont print by default and /p does the print while /!d does the delete

like image 99
aelor Avatar answered Oct 03 '22 22:10

aelor