簡體   English   中英

使用sed在正則表達式模式之間刪除多行文本塊

[英]Deleting a multiline block of text between regex pattern using sed

我有一個重復的文本塊,我需要在一個大的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

我認為這對你有用

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

這將刪除elseendif之間的行

如果你想刪除elseendif也使用這個:

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

在你在評論中提到的情況下試試這個:

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

-n默認情況下不打印, /p執行打印,而/!d執行刪除操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM