Removing a multi-line block of text between a regular expression pattern with sed

I have a duplicate block of text that needs to be deleted in a large XML file. I want to save the first block and delete the second all in one xml tag. For instance:

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

I would like to remove the second block between else and endif and keep the lock between if and else tags. Any help is greatly appreciated - the script completes the deletion of the entire file.

sed -i '/^<!--#else-->/ {p; :a; N; /^\<\!--\#endif--\>/!ba; s/*.\n//}; d' test.xml
+4
source share
1 answer

I think this will work for you.

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

this will delete the lines between elseandendif

if you want to remove elseand endifalso use this:

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

in the case mentioned in the comments, try the following:

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

-n , /p /!d

+4

Source: https://habr.com/ru/post/1569960/


All Articles