5

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
3
  • Obligatory link. Use an XML parsing library. Commented Jan 5, 2015 at 8:18
  • What is the expected output? give a clearer example. Commented Jan 5, 2015 at 8:20
  • The expected output is output everything outside the blocks and the text between #if and #else - i.e. I only want to delete the duplicate text between #else and #endif Commented Jan 5, 2015 at 8:54

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, I thought I had over complicated it. Running it it seems to work, but how can I capture it and output to a new file? -i.bak does not seem to work, it leaves the two files of the same size.
Damn, it's not working for me. If I just run the command above - as it scrolls you can see that the block is deleted. However if I try to output to a new file or use -i.bak I end up with two files of exactly the same size - nothing is deleted. I also tried cat test.xml | sed '/--#else--/,/--#endif--/{//!d}' > new.xml and I end up with two files of exactly the same size.
sed '/--#else--/,/--#endif--/{//!d}' deletes the lines doesnt output anything, use the last command I gave then you will get the result as output in your terminal then you can output it to a file using >
@user2167052 glad that works for you. You can mark the answer as correct if you feel your query has been resolved.
What if you want to delete the #else but not the #endif?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.