我在长行文件示例中有数据:
<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>
我需要这些数据变成以下内容:
<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>
有些行<checkout>
没有数据,不包含/
我该怎么做呢?最好与sed
或perl
答案1
我建议使用工具来缩进您的 xml 文件
xmllint -format -recover nonformatted.xml > formatted.xml
答案2
使用 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>
答案3
sed
@guillermochamorro 打了一点高尔夫球,以避免第一条线断裂
sed -E 's;([^^])(<[^/]);\1\n\2;g' file1