[英]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
這將刪除else
和endif
之間的行
如果你想刪除else
和endif
也使用這個:
sed '/--#else--/,/--#endif--/d' test.xml
在你在評論中提到的情況下試試這個:
sed -n '/--#else--/,/--#endif--/p' test.xml
-n
默認情況下不打印, /p
執行打印,而/!d
執行刪除操作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.