The issue with your sed
command is that $branch_name
contains an embedded newline character. This breaks the syntax of the substitution command in sed
when you inject it into the sed
editing expression.
Using xmlstarlet
to update the name
attribute of the document's Project
root node to be the contents of the branch.txt
file with each newline replaced by a space:
xmlstarlet edit \ --update '/Project/@name' \ --value "$(paste -s -d ' ' branch.txt)" input.file
or, shorter,
xmlstarlet ed \ -u '/Project/@name' \ -v "$(paste -s -d ' ' branch.txt)" input.file
The paste
command in the command substitution reads the branch.txt
file and replaces each newline, apart from the last, with a space character. This results in a string used as the new value for the name
attribute. If you want to retain the final newline character and convert it into a trailing space (as in your expected output), then use tr '\n' ' ' <branch.txt
in place of the paste
command.
The xmlstarlet
utility is invoked with its ed
sub-command. This command edits an XML file, and we specify that we'd like to update a particular element via an XPath query matching the attribute.
Would you need to make the change only when the name
attribute's value is deploy_branch
, then use the XPath query /Project/@name[. = "deploy_branch"]
or /Project[@name = "deploy_branch"]/@name
instead.
The output of the above commands would be
<?xml version="1.0"?> <Project description="first-deployment" name="DEMAND_NAME-CR-1234 DEMAND_NAME-CR-8970" overwrite="true" type="Repository"> </Project>
The xmlstarlet
tool can be made to make an in-place edit if you give it the --inplace
(-L
) option after ed
or edit
. You may avoid having the <?xml ...>
declaration added by using --omit-decl
(-O
).
branch.txt
, which one do you want to use?branch.txt
file do you want to use, or to put the question differently, what do you want your result to look like?