[英]Deleting a multiline block of text between regex pattern using sed
I have a duplicated block of text I need to delete in a large xml file. 我有一个重复的文本块,我需要在一个大的xml文件中删除。 I want to keep the first block and delete the second all within the same xml tag.
我想保留第一个块并在同一个xml标记中删除第二个块。 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. 我想删除else和endif之间的第二个块,并保持if和else标记之间的块。 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
I think this should work for you 我认为这对你有用
sed '/--#else--/,/--#endif--/{//!d}' test.xml
this will delete the lines between else
and endif
这将删除
else
和endif
之间的行
if you want to delete else
and endif
as well use this: 如果你想删除
else
和endif
也使用这个:
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 -n
默认情况下不打印, /p
执行打印,而/!d
执行删除操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.