
Ich habe Daten in einem Beispiel mit langen Zeilen:
<currency>USD</currency><amount>111</amount><amountf>111/amountf><recv_confirms>2</recv_confirms><fee>22</fee><feef>2222</feef><direction>incoming</direction><status>1050</status><sent_to></sent_to><send_txid></send_txid><checkout><type>button</type><invoice>8888</invoice><item_name>Name - Invoice #8888</item_name><src_currency>USD</src_currency><quantity>1</quantity><price_per_item>25.00000000</price_per_item><subtotal>25.00000000</subtotal><total>25.00000000</total>
Ich benötige diese Daten, um Folgendes umzuwandeln:
<currency>USD</currency>
<amount>111</amount>
<amountf>111/amountf>
<recv_confirms>2</recv_confirms>
<fee>22</fee>
<feef>2222</feef>
<direction>incoming</direction>
<status>1050</status>
<sent_to></sent_to>
<send_txid></send_txid>
<checkout>
<type>button</type>
<invoice>8888</invoice>
<item_name>Name - Invoice #8888</item_name>
<src_currency>USD</src_currency>
<quantity>1</quantity>
<price_per_item>25.00000000</price_per_item>
<subtotal>25.00000000</subtotal>
<total>25.00000000</total>
Es gibt Zeilen wie <checkout>
diese, die keine Daten enthalten, die kein/
Wie könnte ich das machen? Am besten mit entweder sed
oderperl
Antwort1
Ich schlage vor, ein Tool zum Einrücken Ihrer XML-Datei zu verwenden
xmllint -format -recover nonformatted.xml > formatted.xml
Antwort2
Mit GNU sed
:
sed -E 's;<([^/]);\n<\1;2g' file
<currency>USD</currency>
<amount>111</amount>
<amountf>111/amountf>
<recv_confirms>2</recv_confirms>
<fee>22</fee>
<feef>2222</feef>
<direction>incoming</direction>
<status>1050</status>
<sent_to></sent_to>
<send_txid></send_txid>
<checkout>
<type>button</type>
<invoice>8888</invoice>
<item_name>Name - Invoice #8888</item_name>
<src_currency>USD</src_currency>
<quantity>1</quantity>
<price_per_item>25.00000000</price_per_item>
<subtotal>25.00000000</subtotal>
<total>25.00000000</total>
Antwort3
Ein bisschen sed
Golf spielen, abseits von @guillermochamorro, um den ersten Zeilenumbruch zu vermeiden
sed -E 's;([^^])(<[^/]);\1\n\2;g' file1