Linux command to insert multiple lines into xml file before match/pattern. Tried with the following command :
#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
No error is shown but the command doesn't work. Any pointers on how to fix it ?
Your sed "a" command is wrong. Change:
sed -i "$toOccurance /a blah blah blah....
to
sed -i "${toOccurance}a blah blah blah....
but @tripleee is right, you could just as well use sed
to find the lines number rather than all the business at the top. But one working script is worth two "it would be better this way" scripts, I guess .
function updateXML () { abcFilePath=abc.xml echo "Updating file $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}a \\n\\\\n\\\\n\\\\n\\n</fileset>\\n</copy>" $abcFilePath
echo "Updated file "
}
updateXML
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.