2017-10-06 376 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>