
У меня есть данные в длинном файле, например:
<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