2017-01-18 48 views
-1

我在XML節點如何更換多字符串的sed在XML

<key>CFBundleShortVersionString</key> 
<string>2.2.1</string> 
<key>CFBundleVersion</key> 
<string>2.2.1</string> 

我想實現這一目標以下幾點:

<key>CFBundleShortVersionString</key> 
<string>$buildno</string> 
<key>CFBundleVersion</key> 
<string>$buildno</string> 

以下命令僅換行符被刪除時工作:

eval "sed -i.bak 's/<key>CFBundleShortVersionString<\/key<string>2.2.*<\/string><key>CFBundleVersion<\/key><string>2.2.*<\/string>/<key>CFBundleShortVersionString<\/key<string>$NUM<\/string><key>CFBundleVersion<\/key><string>$NUM<\/string>/' tiapp.xml" 

我已經嘗試在花括號內使用字符N,但沒有任何運氣。

+0

不要使用文本處理工具'XML'解析,使用像'xmlstarlet'適當'XML'感知工具 – Inian

回答

0

它的工作是這樣的:

sed -i "" "/<key>CFBundleShortVersionString<\/key>/{n;s/<string>.*<\/string>/<string>2.2.$USER<\/string>/;}" tiapp.xml 
sed -i "" "/<key>CFBundleVersion<\/key>/{n;s/<string>.*<\/string>/<string>2.2.$USER<\/string>/;}" tiapp.xml