2017-10-06 178 views
0

我收到了其他人从数据库中提取的xml文件。问题是它包含一些字符串,这些字符串正在以正确的方式创建读取xml的问题。这是它的一个小部分:用xml文件中的空字符串替换字符串

<gmd:fileIdentifier xmlns:gmx="http://www.isotc211.org/2005/gmx">\r\n <gco:CharacterString>0211fa18-e0a4-4d2ed26-7580726e593c</gco:CharacterString>\r\n </gmd:fileIdentifier>\r\n <gmd:language>\r\n <gco:CharacterString>eng</gco:CharacterString>\r\n </gmd:language>\r\n <gmd:hierarchyLevel>\r\n <gmd:MD_ScopeCode codeList="http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml#MD_ScopeCode" codeListValue="dataset" />\r\n </gmd:hierarchyLevel>\r\n <gmd:contact>\r\n <gmd:CI_ResponsibleParty>\r\n  <gmd:organisationName>\r\n  <gco:CharacterString>Research</gco:CharacterString>\r\n  </gmd:organisationName>\r\n  <gmd:contactInfo>\r\n  <gmd:CI_Contact>\r\n   <gmd:address>\r\n   <gmd:CI_Address>\r\n    <gmd:electronicMailAddress>\r\n    <gco:CharacterString>[email protected]</gco:CharacterString>\r\n    </gmd:electronicMailAddress>\r\n   </gmd:CI_Address>\r\n   </gmd:address>\r\n  </gmd:CI_Contact>\r\n  </gmd:contactInfo>\r\n 

正如你可以在每个标签的末尾看到有字符串“\ r \ n”,这就是问题所在。 我尝试使用以下bash命令:

string='\r\n' 
sed -i 's/$string/''/g' test.xml 

,但它不工作,没有空字符串替换$字符串变量。

你能告诉我我做错了什么吗?

在此先感谢

回答

1

您的string变量包含\r\n作为特殊字符序列。但是你需要你在输入文件中直接替换它。

使用以下sed的方法:

sed 's#\\r\\n##g' test.xml 

输出(用于当前输入片段):

<gmd:fileIdentifier xmlns:gmx="http://www.isotc211.org/2005/gmx"> <gco:CharacterString>0211fa18-e0a4-4d2ed26-7580726e593c</gco:CharacterString> </gmd:fileIdentifier> <gmd:language> <gco:CharacterString>eng</gco:CharacterString> </gmd:language> <gmd:hierarchyLevel> <gmd:MD_ScopeCode codeList="http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml#MD_ScopeCode" codeListValue="dataset" /> </gmd:hierarchyLevel> <gmd:contact> <gmd:CI_ResponsibleParty>  <gmd:organisationName>  <gco:CharacterString>Research</gco:CharacterString>  </gmd:organisationName>  <gmd:contactInfo>  <gmd:CI_Contact>   <gmd:address>   <gmd:CI_Address>    <gmd:electronicMailAddress>    <gco:CharacterString>[email protected]</gco:CharacterString>    </gmd:electronicMailAddress>   </gmd:CI_Address>   </gmd:address>  </gmd:CI_Contact>  </gmd:contactInfo> 
+0

非常感谢您的建议!有用!! –

+0

@ sylar_80,不客气 – RomanPerekhrest

1

以下awk可能会帮助你。

awk '{gsub(/\\r\\n/,"")} 1' Input_file 

说明:只需用awk的GSUB实用工具,将在全球范围替代\ r \ n,其中NULL,点这里要注意\ r和\ n被写入到这里消除\特殊意义,它应该把它看作文字,而不是它的特殊含义。 1将打印行。

+1

非常感谢!这是我的方法的一个很好的选择! –

+0

@ sylar_80,欢迎您:-) – RavinderSingh13

1

\r\n是Windows行尾。

我不知道你正在使用哪个XML解析器,或者哪种编程语言,但是试图通过调用dos2unix your-file.xml将文件首先转换为Unix格式,然后将其提供给解析器。您也可以使用普通的文本编辑器进行转换。

希望有所帮助。

+0

我使用的是linux,我尝试过使用dos2unix cmd,但这还不够。由于我可能需要将这种替换替换为大量的文件,不幸的是我必须找到一种自动的方式来完成它。感谢您的提示! –

+0

在没有看到你的文件的情况下,很难说出哪些字节会引起问题,但是我多次遇到它并用'dos2unix'轻松解决。为了在许多文件上做这件事,总会有很好的旧管道和/或for循环。 –

1

\必须进行转义,因为在sed \r顺序改为回车字符

string='\\r\\n' 

也是可变的扩张是双引号之间,但不这样做劲儿引号之间

sed -i "s/$string//g" test.xml 

注:一般来说任何字符串不能使用,因为注射,如果含有/,这是代码生成的一个普遍问题。

+0

是的,你是对的,但即使使用转义字符,如果我使用我的cmd,它也不起作用。 –

1

试试这个:

sed 's/\\r\\n//g' test  #test has the line 


[[email protected] check]$ sed 's/\\r\\n//g' test 
<gmd:fileIdentifier xmlns:gmx="http://www.isotc211.org/2005/gmx"> <gco:CharacterString>0211fa18-e0a4-4d2ed26-7580726e593c</gco:CharacterString> </gmd:fileIdentifier> <gmd:language> <gco:CharacterString>eng</gco:CharacterString> </gmd:language> <gmd:hierarchyLevel> <gmd:MD_ScopeCode codeList="http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/codelist/ML_gmxCodelists.xml#MD_ScopeCode" codeListValue="dataset" /> </gmd:hierarchyLevel> <gmd:contact> <gmd:CI_ResponsibleParty>  <gmd:organisationName>  <gco:CharacterString>Research</gco:CharacterString>  </gmd:organisationName>  <gmd:contactInfo>  <gmd:CI_Contact>   <gmd:address>   <gmd:CI_Address>    <gmd:electronicMailAddress>    <gco:CharacterString>[email protected]</gco:CharacterString>    </gmd:electronicMailAddress>   </gmd:CI_Address>   </gmd:address>  </gmd:CI_Contact>  </gmd:contactInfo>