[英]sed used to replace text between some chars with new text
我有一個問題,我不知道。
我在xml文件中有這樣的內容:
<tag desc="some desc can be different" dep="dep" >value</tag>
我想將其更改為使用sed:
<tag desc="NEW DESC" dep="dep" >value</tag>
我的問題是:我可以使用sed用新的sed替換“ <tag ... >
”之間的文本嗎?
謝謝你的幫助 :)
由於您沒有嘗試真正解析xml sed,因此可以幫助您:
sed -i 's/\(<tag[^>]*[ ]*desc[ ]*\)=[ ]*"[^"]*"/\1="NEW DESC"/g' input.xml
但是,如果您想要一個可靠的解決方案,請使用xmlstarlet
:
xmlstarlet edit -L -u "//tag/@desc" -v "NEW DESC" input.xml
感謝您的快速回答:)
我做了這樣的事情:
typeset repTagdesc='desc="NEW DESC" dep="dep"'
sed -i "s&\(<tag\).*\(>.[0-9]\)&\1 ${repTagdesc}\2&" input.xml
這個解決方案可以嗎? 我對sed不熟悉,這就是為什么我要問。 我檢查它及其作品:
<tag>10</tag>
<tag desc="some desc" dep="dep">20</tag>
<tag desc="other desc" dep="dep">30</tag>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.