简体   繁体   English

使用awk或sed将XML转换为PIPE分隔的输出文件

[英]Convert XML to PIPE delimited output file using awk or sed

How to convert following XML tag into text with pipe delimited file using awk or sed. 如何使用awk或sed将以下XML标记转换为带有管道分隔文件的文本。 I tried with following awk but it didn't return full text from Content type tag. 我尝试跟随awk,但是它没有从Content type标记返回全文。 Any help would great. 任何帮助都很好。

Input_file.dat Input_file.dat

        <entry>
            <updated>2014-05-17T16:34:00-07:00</updated>
                <id>994568497</id>
                <title>No longer usable</title>
                <content type="text">I happen to like the new look, but it crashes with each attempt to use it to perform any real action. Fix it quickly please!.</content>
                <im:contentType term="Application" label="Application"/>
                <im:voteSum>0</im:voteSum>
                <im:voteCount>0</im:voteCount>
                <im:rating>1</im:rating>
                <im:version>4.2.0.165</im:version>
                <author><name>Arcdouble</name><uri>https://test.com/us/reviews/id199894255</uri></author>
        </entry>

Expected output_file.csv format 预期的output_file.csv格式

|2014-05-17T16:34:00-07:00|994568497|No longer usable|I happen to like the new look, but it crashes with each attempt to use it to perform any real action. Fix it quickly please!.|1|Arcdouble|https://test.com/us/reviews/id199894255|

The code below should work for you: 以下代码应为您工作:

perl -ne '/<\/entry>/ && print "\n"; />(.*?)</ && !/<name>/  && print $1."|"; /<name>/ && /name>?(.*?)<\/.*?(uri>?)(.*)?<\/uri/ && print $1."|".$3'

Input: 输入:

tiago@dell:~$ cat file
        <entry>
            <updated>2014-05-17T16:34:00-07:00</updated>
                <id>994568497</id>
                <title>No longer usable</title>
                <content type="text">I happen to like the new look, but it crashes with each attempt to use it to perform any real action. Fix it quickly please!.</content>
                <im:contentType term="Application" label="Application"/>
                <im:voteSum>0</im:voteSum>
                <im:voteCount>0</im:voteCount>
                <im:rating>1</im:rating>
                <im:version>4.2.0.165</im:version>
                <author><name>Arcdouble</name><uri>https://test.com/us/reviews/id199894255</uri></author>
        </entry>
        <entry>
            <updated>2014-05-17T16:34:00-07:00</updated>
                <id>994568497</id>
                <title>No longer usable</title>
                <content type="text">I happen to like the new look, but it crashes with each attempt to use it to perform any real action. Fix it quickly please!.</content>
                <im:contentType term="Application" label="Application"/>
                <im:voteSum>0</im:voteSum>
                <im:voteCount>0</im:voteCount>
                <im:rating>1</im:rating>
                <im:version>4.2.0.165</im:version>
                <author><name>Arcdouble</name><uri>https://test.com/us/reviews/id199894255</uri></author>
        </entry>

Execution: 执行:

tiago@dell:~$ cat file | perl -ne '/<\/entry>/ && print "\n"; />(.*?)</ && !/<name>/  && print $1."|"; /<name>/ && /name>?(.*?)<\/.*?(uri>?)(.*)?<\/uri/ && print $1."|".$3' 
2014-05-17T16:34:00-07:00|994568497|No longer usable|I happen to like the new look, but it crashes with each attempt to use it to perform any real action. Fix it quickly please!.|0|0|1|4.2.0.165|Arcdouble|https://test.com/us/reviews/id199894255
2014-05-17T16:34:00-07:00|994568497|No longer usable|I happen to like the new look, but it crashes with each attempt to use it to perform any real action. Fix it quickly please!.|0|0|1|4.2.0.165|Arcdouble|https://test.com/us/reviews/id199894255

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM