2016-01-24 20 views
0

Linux命令在match/pattern之前將多行插入到xml文件中。嘗試使用以下命令:Linux命令在xml文件中插入多行

#finding pattern: 
toOccurance=`grep -n "\<copy file=\"${appletResource}\/images\/CCMsplash.gif\" toFile=\"${appletbuild}\/splash.gif\" \/>" abc.xml | awk -F: '{print $1}'` 

toOccurance=$(($toOccurance -1)) 

sed -i "$toOccurance /a \<copy todir=\"${controlbuild\}\" flatten=\"true\">\n 
       \<fileset dir=\"${appletResource}\">\n 
         \<include name=\"crop.properties\"\/>\n 
         \<include name=\"vvm.xml\"\/>\n 
         \<exclude name=\"images\/\*splash.gif\"\/> 
       <\/fileset> 
     <\/copy>" abc.xml 

沒有顯示錯誤,但命令不起作用。任何關於如何解決它的指針?

+0

這可能幫助:?如何調試bash腳本(http://unix.stackexchange.com/q/155551/74329) – Cyrus

+1

使用XML解析器來解析XML。顯示一些XML並描述你想要做什麼。 –

+0

下一次你的車壞了,把它拿到車庫,然後告訴機修工'車不行。任何關於如何修復它的指針?並且看看他是如何做出反應的。您可能希望在您的問題中提供更多信息 - 樣本輸入,預期輸出,實際輸出,其他症狀,錯誤消息等。 –

回答

0

你的sed「a」命令是錯誤的。更改:

sed -i "$toOccurance /a blah blah blah.... 

但@tripleee是正確的,你也可以同樣使用sed找到行數,而不是所有的業務在頂部。 但是,一個工作腳本是值得兩個「這將是更好的這種方式」腳本,我猜。在XML文件中指定的匹配/模式之前

-1

更新.....

!/ bin/sh的

功能如updateXML(){ abcFilePath = abc.xml 回聲「更新文件$ abcFilePath 「

if [ -f $abcFilePath ]; 
then 
    echo "File $abcFilePath exists" 
else 
    echo "File $abcFilePath does not exists" 

dos2unix $abcFilePath 
fi 

    toOccurance=`grep -n '\<copy file=\"${appletResource}\/images\/CCMsplash.gif\" toFile=\"${appletbuild}\/splash.gif\" \/>' $abcFilePath | awk -F: '{print $1}'` 

toOccurance=$(($toOccurance - 1)) 

echo "toOccurance = $toOccurance" 

SED -i 」$ {toOccurance}一個\ n \\ñ\\ñ\\ñ\ n < /文件集> \ n < /複製>「 $ abcFilePath

回聲 「更新文件」

}

如updateXML