sample input.xml file

<user dn="CN=company,DN=Department">
<role name="ROLE_ADMIN"/>

output should be

<user dn="String_1,String_2">
<role name="ROLE_ADMIN"/>

Please advice me on the above problem statement. Note: dn shouldn't be hard coded as the value could be blank as well.eg dn=""

  • It will be great if people who downvoted this question, explain why the bad reviews.
    – Edenshaw
    Jun 20, 2017 at 14:34

4 Answers 4


Don't use sed to manipulate XML, use an XML-aware tool like xmlstarlet instead:

xmlstarlet ed -u '/user/@dn' -v 'String1, String2' input.xml

With the command-line XML parser/editor XMLStarlet:

$ company="The Company"
$ department="The Department"

$ dn="CN=$company,DN=$department"

$ xml ed -t -u '/user/@dn' -v "$dn" input.xml
<?xml version="1.0"?>
<user dn="CN=The Company,DN=The Department">
  <role name="ROLE_ADMIN"/>

This command will look up the dn attribute of the root node user and edit its value to be whatever $dn expands to.

For ease of parsing the dn attribute later, it may be worth using a more prominent delimiter between the company name and the following DN in the attribute value. For example a ; rather than a ,. Or add them as separate attributes to the user node.


If the <user...> line is always on its own then this is a simple expression in sed

sed "s/^<user .*/<user dn=\"$string1,$string2\">/" input.xml

Now this was written so that $string1 and $string2 are variables.

So, for example:

$ string1="hello"
$ string2="there"
$ sed "s/^<user .*/<user dn=\"$string1,$string2\">/" input.xml
<user dn="hello,there">
<role name="ROLE_ADMIN"/>

You can use sed -i for an "in place" replacement, which will rewrite input.xml.


sed is your friend

 sed '/^<user dn=/s/CN=[^,]*,DN=[^"]*\"/String_1,String2/' filename

should do it. If you wish String_1 & String_2 to be chosen real-time, you can use variables to achieve this. I would write a script

read -p"Enter string1" str1
read -p"Enter string2" str2
sed -i "/^<user dn=/s/CN=[^,]*,DN=[^\"]*\"/$str1,$str2/" /path/to/input.xml

The -i option modifies the file straightaway.


The double quotes are used in the sed script because they would allow bash variable expansion. Though, for the same reason we have escaped the double quotes that come in the pattern itself.

  • 2
    sed is not your friend when it comes to processing XML. or HTML. or json.
    – cas
    Aug 9, 2016 at 10:35
  • @cas : No doubt there are better/specialized tools like [ xmllint ] . But for this simple task, sed is indeed a good try.
    – sjsam
    Aug 9, 2016 at 10:48
  • 1
    no, using sed or any other line-oriented regexp tool with xml is too fragile - even the smallest changes in input is likely to break the script. If you want to use sed, at least pre-process it with xml2 to convert to a line-oriented format, and then 2xml to convert it back to xml. e.g. xml2 <file.xml | sed ... | 2xml > file.new.xml
    – cas
    Aug 9, 2016 at 11:04

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.