我需要在大型xml文件中删除重复的文本块。我想保留第一个块并在同一个xml标记中删除第二个块。例如:
<!--#if-->
-- several lines of text
<!--#else-->
-- several lines of the same text
<!--#endif-->
我想删除else和endif之间的第二个块,并保持if和else标记之间的块。任何帮助非常感激 - 脚本最终会删除整个文件。
sed -i '/^<!--#else-->/ {p; :a; N; /^\<\!--\#endif--\>/!ba; s/*.\n//}; d' test.xml
答案 0 :(得分:4)
我认为这应该对你有用
sed '/--#else--/,/--#endif--/{//!d}' test.xml
这将删除else
和endif
如果您要删除else
和endif
,请使用此选项:
sed '/--#else--/,/--#endif--/d' test.xml
如果你在评论中提到的话,试试这个:
sed -n '/--#else--/,/--#endif--/p' test.xml
-n
默认情况下不打印,而/p
执行打印时/!d
执行删除