0

I am using sed to replace a content in a file with string "dba01upc\Fusion_test". After the replacement I see the '\' character is missing. The replaced string is dba01upcFusion_test . Looks like sed is ignoring '\' while replacing.. Can anyone let me know the sed command to include all characters?

My Sed Command:

sed -i "s%{"sara_ftp_username"}%"dba01upc\Fusion_test"%g" /home_ldap/user1/placeholder/Sara.xml

Before Replacement : Sara.xml

<?xml version="1.0" encoding="UTF-8"?>
<ser:service-account >
<ser:description/>
<ser:static-account>
<con:username>{sara_ftp_username}</con:username>
</ser:static-account>
</ser:service-account>

After Replacement : Sara.xml

<?xml version="1.0" encoding="UTF-8"?>
<ser:service-account>
<ser:description/>
<ser:static-account>
<con:username>dba01upcFusion_test</con:username>
</ser:static-account>
</ser:service-account>

Thanks

Tech Tech
  • 141
  • 2
  • 10

2 Answers2

3
sed -i 's%{sara_ftp_username}%dba01upc[\]Fusion_test%g' /home_ldap/user1/placeholder/Sara.xml
# or
sed -i 's%{sara_ftp_username}%dba01upc\\Fusion_test%g' /home_ldap/user1/placeholder/Sara.xml
# or
sed -i "s%{sara_ftp_username}%dba01upc\\\Fusion_test%g" /home_ldap/user1/placeholder/Sara.xml

escape the \ (twice if double quote)

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
1

Try this:

sed -i "s%sara_ftp_username%dba01upc\\\Fusion_test%g" /home_ldap/user1/placeholder/Sara.xml
Technext
  • 7,887
  • 9
  • 48
  • 76
  • 1
    Why three backslashes rather than two or four? – ivan_pozdeev Jul 22 '14 at 12:26
  • Thank you very much for your answer.. I have another situation.. I am using the environment variable in sed command and I am getting the same problem(the "\" is not getting replaced).. export value="dba01upc\Fusion_test" sed -i "s%{"sara_ftp_username"}%$value%g" /home_ldap/user1/placeholder/Sara.xml can u pls let me know if you have any suggestion to fix this ? – Tech Tech Jul 22 '14 at 12:58
  • Use this: `export value="dba01upc\\\Fusion_test" sed -i "s%sara_ftp_username%$value%g" /home_ldap/user1/placeholder/Sara.xml` – Technext Jul 22 '14 at 13:06
  • @ivan_pozdeev: Please refer this: http://stackoverflow.com/questions/2369314/why-does-sed-require-3-backslashes-for-a-regular-backslash – Technext Jul 22 '14 at 13:07