sed命令没有替换" \"

时间:2014-07-22 11:50:35

标签: bash shell unix sed

我使用sed用字符串“dba01upc \ Fusion_test”替换文件中的内容。更换后,我看到'\'字符丢失了。替换后的字符串是dba01upcFusion_test。看起来sed在替换时忽略了'\' 谁能让我知道sed命令包含所有字符?

我的Sed命令:

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

替换前: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>

替换后: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>

由于

2 个答案:

答案 0 :(得分: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

转义\(两倍,如果双引号)

答案 1 :(得分:1)

试试这个:

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