priyanka priyanka - 1 year ago 94
Bash Question

replace the contents of an xml file with sed

i have one file which looks something like this

<variable name="VOS_BRANCH">
<variable name="VOS_BRANCH_LABEL">

so now here i want to replace the values CER_FREEZE_12_0_0_98000_6_CCT and cer_mainline to some other value that i am passing as a parameter to the script.

what i am doing now is

xml ed -u "/ENV_VARIABLES/variable[@name='VOS_BRANCH']/value" -v $new_branch_name $file > $file1
mv $file1 $file
xml ed -u "/ENV_VARIABLES/variable[@name='VOS_BRANCH_LABEL']/value" -v $new_branch_label $file > $file1
mv $file1 $file

but now the requirement is to use sed to do this operation.
and i am not sure how to use sed here

Answer Source

Try this:

sed "
    /<variable name=\"VOS_BRANCH\">/,/<\/variable>/ s@<value>.*</value>@<value>$new_branch_name</value>@;
    /<variable name=\"VOS_BRANCH_LABEL\">/,/<\/variable>/ s@<value>.*</value>@<value>$new_branch_label</value>@
" "$file"

That will write the new content to the terminal. If you're satisfied, you can either add > "$file1" as you had before, or you can add the -i option to sed to edit the file in-place.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download