2017-10-06 21 views
0

DBから他の人が抽出した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 

出力(あなたの現在の入力フラグメントのために):sedの中\rシーケンスがキャリッジリターンに変更されているため

<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 

説明:単に\ rと\ nは、ここでは\特別な意味をなくすために書かれている、それはすべきNULLで代用します\ r \ nをグローバルになるのawkのGSUBユーティリティ、ポイントここで注目されることを利用しそれはリテラルなキャラクターであり、それは特別な意味ではありません。 1行が印刷されます。

+1

おかげでたくさん!それは私のアプローチの良い選択肢です! –

+0

@ sylar_80、あなたの歓迎:-) – RavinderSingh13

1

\r\nは、Windowsの行末です。

使用しているXMLパーサーやプログラミング言語はわかりませんが、dos2unix your-file.xmlを呼び出してファイルをUnix形式に変換してからパーサにフィードしてみてください。一般的なテキストエディタで変換することもできます。

希望に役立ちます。

+0

私はLinuxを使っていますが、dos2unix cmdを使ってみましたが、それだけでは不十分でした。私はこのファイルを大量のファイルに置き換えなければならないかもしれないので、残念ながら私はそれを行うための自動方法を見つけなければなりません。あなたのヒントありがとう! –

+0

あなたのファイルを見ることなく、どのバイトが問題を引き起こしているのかを少し難しくしていますが、私はそれを何度か打ち倒して 'dos2unix'で簡単に解決しました。多くのファイルでそれを行うためには、いつも良い古いパイプやforループがあります。 –

1

\はエスケープする必要があります文字

string='\\r\\n' 

も変数の展開は、二重引用符の間で行われているが、/が含まれている場合、一般的に注意

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

ACシングル引用符の間の任意の文字列があるため、注射を使用することができないではない、これはコード生成の一般的な問題です。

+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> 
関連する問題