So I am using sed to find and replace certain strings:

Case 1. A value in the file is always constant. And I want to replace the parameter from false to true. So I use this command:

    sed -i -e 's/entry key="ignore_vc_cert" value="false"/entry key="ignore_vc_cert" value="true"/g'  /usr/local/avamar/var/mc/server_data/prefs/mcserver.xml

This will find ignore_vc_cert and change its value from false to true. Works good.

Next,I have another file which has the following parameter:


Now, this value here can change depending on deployment. I want to replace the number here, 4, or whatever with the output of df -h | grep data0* | wc -l

So if output of df -h | grep data0* | wc -l is 3 it should replace the value for numberOfDisk Parameter from 4 to 3

I wrote this command:

sed -e s/"$(cat /usr/local/vdr/etc/vdr-configuration.xml  | grep -i numberOfDisk)"/<numberOfDisk>"$(df -h | grep data0* | wc -l)"</numberOfDisk>/g > /usr/local/vdr/etc/vdr-configuration.xml

But form some reason, I get numberOfDisk: No such file or directory

If I do:

sed -e 's/"$(cat /usr/local/vdr/etc/vdr-configuration.xml  | grep -i numberofdisk)"/<numberOfDisk>"$(df -h | grep data0* | wc -l)"</numberOfDisk>/g' /usr/local/vdr/etc/vdr-configuration.xml

Then I get: sed: -e expression #1, char 15: unknown option tos'`

Any inputs on how I could fix this?

2 Answers 2


To manipulate XML documents use XML parsers/tools.

I would go with xmlstarlet.

grep -c 'data0*' - will output the number of matched lines, no need to pipe with wc -l

xmlstarlet ed -u "//numberOfDisk" -v $(df -h | grep -c 'data0*') /usr/local/vdr/etc/vdr-configuration.xml > /tmp/tmp-test.xml 
           && mv /tmp/tmp-test.xml /usr/local/vdr/etc/vdr-configuration.xml

ed - edit mode

-u (--update) - update xml document


Try this. Grabs all the stuff before the existing number of disks into \1, grabs the stuff after into \2. And then prints \1 followed by the df ... output, followed by \2.

sed -e 's/\(^.*numberOfDisk>\)[0-9]*\(<.*\)/\1'$(df -h | grep -c data0)'\2/' vdr-configuration.xml

Another (shorter) approach

sed -e '/numberOf/s/[0-9]\+/'$(df -h|grep -c data0)'/' vdr-configuration.xml

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.