我有一个固定长度的文件,如何将数据提取为看起来像 csv 文件

我有一个固定长度的文件,如何将数据提取为看起来像 csv 文件

所以我有以下文件:

HDR2013100120131001000000000000                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
BDY         1         1<record><entry unit="Baneling"><name>ARMOR</name><value>0</value></entry><entry unit="Baneling"><name>BUILD_TIME</name><value>20</value></entry><entry unit="Baneling"><name>GAS</name><value>25</value></entry><entry unit="Baneling"><name>HP</name><value>30</value></entry><entry unit="Baneling"><name>MINERAL</name><value>75</value></entry><entry unit="Baneling"><name>POP</name><value>.50</value></entry><entry unit="Baneling"><name>SPEED</name><value>2.50</value></entry><entry unit="Baneling"><name
BDY         1         2>TYPE</name><value>-</value></entry><entry unit="Baneling"><name>UPG_SPEED</name><value>2.95</value></entry><entry unit="Brood Lord"><name>ARMOR</name><value>1</value></entry><entry unit="Brood Lord"><name>BUILD_TIME</name><value>34</value></entry><entry unit="Brood Lord"><name>GAS</name><value>250</value></entry><entry unit="Brood Lord"><name>HP</name><value>225</value></entry><entry unit="Brood Lord"><name>MINERAL</name><value>300</value></entry><entry unit="Brood Lord"><name>POP</name><value>
BDY         1         34.00</value></entry><entry unit="Brood Lord"><name>SPEED</name><value>1.41</value></entry><entry unit="Brood Lord"><name>TYPE</name><value>Armored / Massive</value></entry><entry unit="Brood Lord"><name>UPG_SPEED</name><value>1.41</value></entry><entry unit="Broodling"><name>ARMOR</name><value>0</value></entry><entry unit="Broodling"><name>BUILD_TIME</name><value>1</value></entry><entry unit="Broodling"><name>GAS</name><value>0</value></entry><entry unit="Broodling"><name>HP</name><value>30</val
BDY         1         4ue></entry><entry unit="Broodling"><name>MINERAL</name><value>0</value></entry><entry unit="Broodling"><name>POP</name><value>.00</value></entry><entry unit="Broodling"><name>SPEED</name><value>3.84</value></entry><entry unit="Broodling"><name>TYPE</name><value>Light</value></entry><entry unit="Broodling"><name>UPG_SPEED</name><value>3.84</value></entry><entry unit="Changeling"><name>ARMOR</name><value>0</value></entry><entry unit="Changeling"><name>BUILD_TIME</name><value>1</value></entry><entr
BDY         1         5y unit="Changeling"><name>GAS</name><value>0</value></entry><entry unit="Changeling"><name>HP</name><value>5</value></entry><entry unit="Changeling"><name>MINERAL</name><value>0</value></entry><entry unit="Changeling"><name>POP</name><value>.00</value></entry><entry unit="Changeling"><name>SPEED</name><value>2.25</value></entry><entry unit="Changeling"><name>TYPE</name><value>Light</value></entry><entry unit="Changeling"><name>UPG_SPEED</name><value>2.25</value></entry><entry unit="Corruptor"><n
BDY         1         6ame>ARMOR</name><value>2</value></entry><entry unit="Corruptor"><name>BUILD_TIME</name><value>40</value></entry><entry unit="Corruptor"><name>GAS</name><value>100</value></entry><entry unit="Corruptor"><name>HP</name><value>200</value></entry><entry unit="Corruptor"><name>MINERAL</name><value>150</value></entry><entry unit="Corruptor"><name>POP</name><value>2.00</value></entry><entry unit="Corruptor"><name>SPEED</name><value>2.95</value></entry><entry unit="Corruptor"><name>TYPE</name><value>Arm
BDY         1         7ored</value></entry><entry unit="Corruptor"><name>UPG_SPEED</name><value>2.95</value></entry><entry unit="Drone"><name>ARMOR</name><value>0</value></entry><entry unit="Drone"><name>BUILD_TIME</name><value>17</value></entry><entry unit="Drone"><name>GAS</name><value>0</value></entry><entry unit="Drone"><name>HP</name><value>40</value></entry><entry unit="Drone"><name>MINERAL</name><value>50</value></entry><entry unit="Drone"><name>POP</name><value>1.00</value></entry><entry unit="Drone"><name>SPE
BDY         1         8ED</name><value>2.81</value></entry><entry unit="Drone"><name>TYPE</name><value>Light</value></entry><entry unit="Drone"><name>UPG_SPEED</name><value>2.81</value></entry><entry unit="Hydralisk"><name>ARMOR</name><value>0</value></entry><entry unit="Hydralisk"><name>BUILD_TIME</name><value>33</value></entry><entry unit="Hydralisk"><name>GAS</name><value>50</value></entry><entry unit="Hydralisk"><name>HP</name><value>80</value></entry><entry unit="Hydralisk"><name>MINERAL</name><value>100</value><
BDY         1         9/entry><entry unit="Hydralisk"><name>POP</name><value>2.00</value></entry><entry unit="Hydralisk"><name>SPEED</name><value>2.25</value></entry><entry unit="Hydralisk"><name>TYPE</name><value>Light</value></entry><entry unit="Hydralisk"><name>UPG_SPEED</name><value>2.25</value></entry><entry unit="Infested Terran"><name>ARMOR</name><value>0</value></entry><entry unit="Infested Terran"><name>BUILD_TIME</name><value>50</value></entry><entry unit="Infested Terran"><name>GAS</name><value>0</value></e
BDY         1        10ntry><entry unit="Infested Terran"><name>HP</name><value>50</value></entry><entry unit="Infested Terran"><name>MINERAL</name><value>0</value></entry><entry unit="Infested Terran"><name>POP</name><value>.00</value></entry><entry unit="Infested Terran"><name>SPEED</name><value>1.50</value></entry><entry unit="Infested Terran"><name>TYPE</name><value>-</value></entry><entry unit="Infested Terran"><name>UPG_SPEED</name><value>1.50</value></entry><entry unit="Infestor"><name>ARMOR</name><value>0</val
BDY         1        11ue></entry><entry unit="Infestor"><name>BUILD_TIME</name><value>50</value></entry><entry unit="Infestor"><name>GAS</name><value>150</value></entry><entry unit="Infestor"><name>HP</name><value>90</value></entry><entry unit="Infestor"><name>MINERAL</name><value>100</value></entry><entry unit="Infestor"><name>POP</name><value>2.00</value></entry><entry unit="Infestor"><name>SPEED</name><value>2.25</value></entry><entry unit="Infestor"><name>TYPE</name><value>Armored</value></entry><entry unit="Infe
BDY         1        12stor"><name>UPG_SPEED</name><value>2.25</value></entry><entry unit="Locust"><name>ARMOR</name><value>0</value></entry><entry unit="Locust"><name>BUILD_TIME</name><value>5</value></entry><entry unit="Locust"><name>GAS</name><value>0</value></entry><entry unit="Locust"><name>HP</name><value>65</value></entry><entry unit="Locust"><name>MINERAL</name><value>0</value></entry><entry unit="Locust"><name>POP</name><value>.00</value></entry><entry unit="Locust"><name>SPEED</name><value>1.88</value></entr
BDY         1        13y><entry unit="Locust"><name>TYPE</name><value>Light</value></entry><entry unit="Locust"><name>UPG_SPEED</name><value>1.88</value></entry><entry unit="Mutalisk"><name>ARMOR</name><value>0</value></entry><entry unit="Mutalisk"><name>BUILD_TIME</name><value>33</value></entry><entry unit="Mutalisk"><name>GAS</name><value>100</value></entry><entry unit="Mutalisk"><name>HP</name><value>120</value></entry><entry unit="Mutalisk"><name>MINERAL</name><value>100</value></entry><entry unit="Mutalisk"><name
BDY         1        14>POP</name><value>2.00</value></entry><entry unit="Mutalisk"><name>SPEED</name><value>3.75</value></entry><entry unit="Mutalisk"><name>TYPE</name><value>Light</value></entry><entry unit="Mutalisk"><name>UPG_SPEED</name><value>3.75</value></entry><entry unit="Overlord"><name>ARMOR</name><value>0</value></entry><entry unit="Overlord"><name>BUILD_TIME</name><value>25</value></entry><entry unit="Overlord"><name>GAS</name><value>0</value></entry><entry unit="Overlord"><name>HP</name><value>200</value
BDY         1        15></entry><entry unit="Overlord"><name>MINERAL</name><value>100</value></entry><entry unit="Overlord"><name>POP</name><value>.00</value></entry><entry unit="Overlord"><name>SPEED</name><value>.59</value></entry><entry unit="Overlord"><name>TYPE</name><value>Armored</value></entry><entry unit="Overlord"><name>UPG_SPEED</name><value>1.88</value></entry><entry unit="Overseer"><name>ARMOR</name><value>0</value></entry><entry unit="Overseer"><name>BUILD_TIME</name><value>17</value></entry><entry unit=
BDY         1        16"Overseer"><name>GAS</name><value>50</value></entry><entry unit="Overseer"><name>HP</name><value>200</value></entry><entry unit="Overseer"><name>MINERAL</name><value>150</value></entry><entry unit="Overseer"><name>POP</name><value>.00</value></entry><entry unit="Overseer"><name>SPEED</name><value>1.88</value></entry><entry unit="Overseer"><name>TYPE</name><value>Armored</value></entry><entry unit="Overseer"><name>UPG_SPEED</name><value>2.75</value></entry><entry unit="Queen"><name>ARMOR</name><v
BDY         1        17alue>1</value></entry><entry unit="Queen"><name>BUILD_TIME</name><value>50</value></entry><entry unit="Queen"><name>GAS</name><value>0</value></entry><entry unit="Queen"><name>HP</name><value>175</value></entry><entry unit="Queen"><name>MINERAL</name><value>150</value></entry><entry unit="Queen"><name>POP</name><value>2.00</value></entry><entry unit="Queen"><name>SPEED</name><value>.94</value></entry><entry unit="Queen"><name>TYPE</name><value>-</value></entry><entry unit="Queen"><name>UPG_SPEED
BDY         1        18</name><value>.94</value></entry><entry unit="Roach"><name>ARMOR</name><value>1</value></entry><entry unit="Roach"><name>BUILD_TIME</name><value>27</value></entry><entry unit="Roach"><name>GAS</name><value>25</value></entry><entry unit="Roach"><name>HP</name><value>145</value></entry><entry unit="Roach"><name>MINERAL</name><value>75</value></entry><entry unit="Roach"><name>POP</name><value>2.00</value></entry><entry unit="Roach"><name>SPEED</name><value>2.25</value></entry><entry unit="Roach"><n
BDY         1        19ame>TYPE</name><value>Armored</value></entry><entry unit="Roach"><name>UPG_SPEED</name><value>3.00</value></entry><entry unit="Spine Crawler"><name>ARMOR</name><value>2</value></entry><entry unit="Spine Crawler"><name>BUILD_TIME</name><value>50</value></entry><entry unit="Spine Crawler"><name>GAS</name><value>0</value></entry><entry unit="Spine Crawler"><name>HP</name><value>300</value></entry><entry unit="Spine Crawler"><name>MINERAL</name><value>150</value></entry><entry unit="Spine Crawler"><
BDY         1        20name>POP</name><value>.00</value></entry><entry unit="Spine Crawler"><name>SPEED</name><value>1.00</value></entry><entry unit="Spine Crawler"><name>TYPE</name><value>Armored / Structure</value></entry><entry unit="Spine Crawler"><name>UPG_SPEED</name><value>1.00</value></entry><entry unit="Spore Crawler"><name>ARMOR</name><value>1</value></entry><entry unit="Spore Crawler"><name>BUILD_TIME</name><value>30</value></entry><entry unit="Spore Crawler"><name>GAS</name><value>0</value></entry><entry u
BDY         1        21nit="Spore Crawler"><name>HP</name><value>400</value></entry><entry unit="Spore Crawler"><name>MINERAL</name><value>150</value></entry><entry unit="Spore Crawler"><name>POP</name><value>.00</value></entry><entry unit="Spore Crawler"><name>SPEED</name><value>1.00</value></entry><entry unit="Spore Crawler"><name>TYPE</name><value>Armored / Structure</value></entry><entry unit="Spore Crawler"><name>UPG_SPEED</name><value>1.00</value></entry><entry unit="Swarm Host"><name>ARMOR</name><value>1</value
BDY         1        22></entry><entry unit="Swarm Host"><name>BUILD_TIME</name><value>40</value></entry><entry unit="Swarm Host"><name>GAS</name><value>100</value></entry><entry unit="Swarm Host"><name>HP</name><value>160</value></entry><entry unit="Swarm Host"><name>MINERAL</name><value>200</value></entry><entry unit="Swarm Host"><name>POP</name><value>3.00</value></entry><entry unit="Swarm Host"><name>SPEED</name><value>2.25</value></entry><entry unit="Swarm Host"><name>TYPE</name><value>Armored</value></entry><ent
BDY         1        23ry unit="Swarm Host"><name>UPG_SPEED</name><value>2.25</value></entry><entry unit="Ultralisk"><name>ARMOR</name><value>1</value></entry><entry unit="Ultralisk"><name>BUILD_TIME</name><value>55</value></entry><entry unit="Ultralisk"><name>GAS</name><value>200</value></entry><entry unit="Ultralisk"><name>HP</name><value>500</value></entry><entry unit="Ultralisk"><name>MINERAL</name><value>300</value></entry><entry unit="Ultralisk"><name>POP</name><value>6.00</value></entry><entry unit="Ultralisk">
BDY         1        24<name>SPEED</name><value>2.95</value></entry><entry unit="Ultralisk"><name>TYPE</name><value>Armored / Massive</value></entry><entry unit="Ultralisk"><name>UPG_SPEED</name><value>2.95</value></entry><entry unit="Viper"><name>ARMOR</name><value>1</value></entry><entry unit="Viper"><name>BUILD_TIME</name><value>40</value></entry><entry unit="Viper"><name>GAS</name><value>200</value></entry><entry unit="Viper"><name>HP</name><value>150</value></entry><entry unit="Viper"><name>MINERAL</name><value>1
BDY         1        2500</value></entry><entry unit="Viper"><name>POP</name><value>3.00</value></entry><entry unit="Viper"><name>SPEED</name><value>2.95</value></entry><entry unit="Viper"><name>TYPE</name><value>Armored</value></entry><entry unit="Viper"><name>UPG_SPEED</name><value>2.95</value></entry><entry unit="Zergling"><name>ARMOR</name><value>0</value></entry><entry unit="Zergling"><name>BUILD_TIME</name><value>24</value></entry><entry unit="Zergling"><name>GAS</name><value>0</value></entry><entry unit="Zergli
BDY         1        26ng"><name>HP</name><value>35</value></entry><entry unit="Zergling"><name>MINERAL</name><value>25</value></entry><entry unit="Zergling"><name>POP</name><value>.50</value></entry><entry unit="Zergling"><name>SPEED</name><value>2.95</value></entry><entry unit="Zergling"><name>TYPE</name><value>Light</value></entry><entry unit="Zergling"><name>UPG_SPEED</name><value>4.70</value></entry></record>                                                               
BDY         2         1<record><entry unit="Archon"><name>ARMOR</name><value>0</value></entry><entry unit="Archon"><name>BUILD_TIME</name><value>12</value></entry><entry unit="Archon"><name>GAS</name><value>300</value></entry><entry unit="Archon"><name>HP</name><value>10</value></entry><entry unit="Archon"><name>MINERAL</name><value></value></entry><entry unit="Archon"><name>POP</name><value>4.00</value></entry><entry unit="Archon"><name>SHIELD</name><value>350</value></entry><entry unit="Archon"><name>SPEED</name><va
BDY         2         2lue>2.81</value></entry><entry unit="Archon"><name>TOTAL</name><value>360</value></entry><entry unit="Archon"><name>TYPE</name><value>Massive</value></entry><entry unit="Archon"><name>UPG_SPEED</name><value>2.81</value></entry><entry unit="Carrier (4 interc.)"><name>ARMOR</name><value>2</value></entry><entry unit="Carrier (4 interc.)"><name>BUILD_TIME</name><value>120</value></entry><entry unit="Carrier (4 interc.)"><name>GAS</name><value>250</value></entry><entry unit="Carrier (4 interc.)"><nam
BDY         2         3e>HP</name><value>300</value></entry><entry unit="Carrier (4 interc.)"><name>MINERAL</name><value></value></entry><entry unit="Carrier (4 interc.)"><name>POP</name><value>6.00</value></entry><entry unit="Carrier (4 interc.)"><name>SHIELD</name><value>150</value></entry><entry unit="Carrier (4 interc.)"><name>SPEED</name><value>1.88</value></entry><entry unit="Carrier (4 interc.)"><name>TOTAL</name><value>450</value></entry><entry unit="Carrier (4 interc.)"><name>TYPE</name><value>Armored / Massi
BDY         2         4ve</value></entry><entry unit="Carrier (4 interc.)"><name>UPG_SPEED</name><value>1.88</value></entry><entry unit="Colossus"><name>ARMOR</name><value>1</value></entry><entry unit="Colossus"><name>BUILD_TIME</name><value>75</value></entry><entry unit="Colossus"><name>GAS</name><value>200</value></entry><entry unit="Colossus"><name>HP</name><value>200</value></entry><entry unit="Colossus"><name>MINERAL</name><value></value></entry><entry unit="Colossus"><name>POP</name><value>6.00</value></entry><e
BDY         2         5ntry unit="Colossus"><name>SHIELD</name><value>150</value></entry><entry unit="Colossus"><name>SPEED</name><value>2.25</value></entry><entry unit="Colossus"><name>TOTAL</name><value>350</value></entry><entry unit="Colossus"><name>TYPE</name><value>Armored / Massive</value></entry><entry unit="Colossus"><name>UPG_SPEED</name><value>2.25</value></entry><entry unit="Dark Templar"><name>ARMOR</name><value>1</value></entry><entry unit="Dark Templar"><name>BUILD_TIME</name><value>55</value></entry><en
BDY         2         6try unit="Dark Templar"><name>GAS</name><value>125</value></entry><entry unit="Dark Templar"><name>HP</name><value>40</value></entry><entry unit="Dark Templar"><name>MINERAL</name><value></value></entry><entry unit="Dark Templar"><name>POP</name><value>2.00</value></entry><entry unit="Dark Templar"><name>SHIELD</name><value>80</value></entry><entry unit="Dark Templar"><name>SPEED</name><value>2.81</value></entry><entry unit="Dark Templar"><name>TOTAL</name><value>120</value></entry><entry unit="
BDY         2         7Dark Templar"><name>TYPE</name><value>-</value></entry><entry unit="Dark Templar"><name>UPG_SPEED</name><value>2.81</value></entry><entry unit="High Templar"><name>ARMOR</name><value>0</value></entry><entry unit="High Templar"><name>BUILD_TIME</name><value>55</value></entry><entry unit="High Templar"><name>GAS</name><value>150</value></entry><entry unit="High Templar"><name>HP</name><value>40</value></entry><entry unit="High Templar"><name>MINERAL</name><value></value></entry><entry unit="High T
BDY         2         8emplar"><name>POP</name><value>2.00</value></entry><entry unit="High Templar"><name>SHIELD</name><value>40</value></entry><entry unit="High Templar"><name>SPEED</name><value>1.88</value></entry><entry unit="High Templar"><name>TOTAL</name><value>80</value></entry><entry unit="High Templar"><name>TYPE</name><value>-</value></entry><entry unit="High Templar"><name>UPG_SPEED</name><value>1.88</value></entry><entry unit="Immortal"><name>ARMOR</name><value>1</value></entry><entry unit="Immortal"><nam
BDY         2         9e>BUILD_TIME</name><value>50</value></entry><entry unit="Immortal"><name>GAS</name><value>100</value></entry><entry unit="Immortal"><name>HP</name><value>200</value></entry><entry unit="Immortal"><name>MINERAL</name><value></value></entry><entry unit="Immortal"><name>POP</name><value>4.00</value></entry><entry unit="Immortal"><name>SHIELD</name><value>100</value></entry><entry unit="Immortal"><name>SPEED</name><value>2.25</value></entry><entry unit="Immortal"><name>TOTAL</name><value>300</value>
BDY         2        10</entry><entry unit="Immortal"><name>TYPE</name><value>Armored</value></entry><entry unit="Immortal"><name>UPG_SPEED</name><value>2.25</value></entry><entry unit="Interceptor"><name>ARMOR</name><value>0</value></entry><entry unit="Interceptor"><name>BUILD_TIME</name><value>8</value></entry><entry unit="Interceptor"><name>GAS</name><value>0</value></entry><entry unit="Interceptor"><name>HP</name><value>40</value></entry><entry unit="Interceptor"><name>MINERAL</name><value></value></entry><entry u
BDY         2        11nit="Interceptor"><name>POP</name><value>.00</value></entry><entry unit="Interceptor"><name>SHIELD</name><value>40</value></entry><entry unit="Interceptor"><name>SPEED</name><value>7.50</value></entry><entry unit="Interceptor"><name>TOTAL</name><value>80</value></entry><entry unit="Interceptor"><name>TYPE</name><value>Light</value></entry><entry unit="Interceptor"><name>UPG_SPEED</name><value>7.50</value></entry><entry unit="Mothership"><name>ARMOR</name><value>2</value></entry><entry unit="Moth
BDY         2        12ership"><name>BUILD_TIME</name><value>160</value></entry><entry unit="Mothership"><name>GAS</name><value>400</value></entry><entry unit="Mothership"><name>HP</name><value>350</value></entry><entry unit="Mothership"><name>MINERAL</name><value></value></entry><entry unit="Mothership"><name>POP</name><value>8.00</value></entry><entry unit="Mothership"><name>SHIELD</name><value>350</value></entry><entry unit="Mothership"><name>SPEED</name><value>1.41</value></entry><entry unit="Mothership"><name>TOT
BDY         2        13AL</name><value>700</value></entry><entry unit="Mothership"><name>TYPE</name><value>Massive</value></entry><entry unit="Mothership"><name>UPG_SPEED</name><value>1.41</value></entry><entry unit="Mothership Core"><name>ARMOR</name><value>1</value></entry><entry unit="Mothership Core"><name>BUILD_TIME</name><value>30</value></entry><entry unit="Mothership Core"><name>GAS</name><value>100</value></entry><entry unit="Mothership Core"><name>HP</name><value>130</value></entry><entry unit="Mothership Co
BDY         2        14re"><name>MINERAL</name><value></value></entry><entry unit="Mothership Core"><name>POP</name><value>2.00</value></entry><entry unit="Mothership Core"><name>SHIELD</name><value>60</value></entry><entry unit="Mothership Core"><name>SPEED</name><value>1.88</value></entry><entry unit="Mothership Core"><name>TOTAL</name><value>190</value></entry><entry unit="Mothership Core"><name>TYPE</name><value>Armored</value></entry><entry unit="Mothership Core"><name>UPG_SPEED</name><value>1.88</value></entry><
BDY         2        15entry unit="Observer"><name>ARMOR</name><value>0</value></entry><entry unit="Observer"><name>BUILD_TIME</name><value>30</value></entry><entry unit="Observer"><name>GAS</name><value>75</value></entry><entry unit="Observer"><name>HP</name><value>40</value></entry><entry unit="Observer"><name>MINERAL</name><value></value></entry><entry unit="Observer"><name>POP</name><value>1.00</value></entry><entry unit="Observer"><name>SHIELD</name><value>20</value></entry><entry unit="Observer"><name>SPEED</nam
BDY         2        16e><value>1.88</value></entry><entry unit="Observer"><name>TOTAL</name><value>60</value></entry><entry unit="Observer"><name>TYPE</name><value>Light</value></entry><entry unit="Observer"><name>UPG_SPEED</name><value>2.81</value></entry><entry unit="Oracle"><name>ARMOR</name><value>0</value></entry><entry unit="Oracle"><name>BUILD_TIME</name><value>50</value></entry><entry unit="Oracle"><name>GAS</name><value>150</value></entry><entry unit="Oracle"><name>HP</name><value>100</value></entry><entry u
BDY         2        17nit="Oracle"><name>MINERAL</name><value></value></entry><entry unit="Oracle"><name>POP</name><value>3.00</value></entry><entry unit="Oracle"><name>SHIELD</name><value>60</value></entry><entry unit="Oracle"><name>SPEED</name><value>3.38</value></entry><entry unit="Oracle"><name>TOTAL</name><value>160</value></entry><entry unit="Oracle"><name>TYPE</name><value>Light</value></entry><entry unit="Oracle"><name>UPG_SPEED</name><value>3.38</value></entry><entry unit="Phoenix"><name>ARMOR</name><value>0
...                                                                                                                                                                                                                                                                                                 

由于它太大,更多的东西无法粘贴整个内容。

XML 不正确,但如果您按照上一行操作,您可以看到当到达第 524 个字符时,xml 在下一行连续。

文件结构为:

Header:

Rec_type string(3) ‘HDR’;

Data_date date(‘YYYYMMDD’);

Create_timestamp date(‘YYYYMMDDHH24MMSSNNNNNN);

Filler string(492);

Newline string(1)=”\n”;

Total length: 524

Body:

Rec_type string(3) ‘BDY’

ID_XML decimal(10);

ID_SEGMENT decimal(10);

XML_DATA string(500);

Newline string(1)=”\n”

Total length: 524

Trailer:

Rec_type string(3) ‘TRL’

Num_records Decimal(9); //Note: leftpaded with leading zeros, number of body records

Num_xml_entries Decimal(9); //Note: leftpaded with leading zeros, number of xml <entry >…</entry> tags

Filler string(502);

Newline string(1)=”\n”;

Total length: 524

我只需要来自正文的数据将其提取到如下所示的新文件中:

ID_XML     ID_SEGMENT    UNIT         NAME           VALUE

1          1            Baneling      SPEED          2.50

1          1            Baneling      TYPE           null
...

1          2            Baneling      UPG_SPEED      2.95
... and so on

其中 ID_XML 是 BDY 之后的第一个数字,ID_Segment 是后面的数字。单位/名称和值必须取自 XML 段。 ps 这只是视觉表示实际上应该像 CSV

1,1,Baneling,SPEED,2.50

1,1,Baneling,TYPE,null
...

1,2,Baneling,UPG_SPEED,2.95
... and so on

到目前为止,仅提取我使用的段之后:

{
   sed  -e 's/></>\n</g' \
        -e 's/<value>\n<\/value>/<value>null<\/value>/g' \
        -e 's/<record>//g' \
        -e 's/<\/record>//g'
        -e 's/<entry unit="//g'\
        -e 's/">/,/g'\
        -e 's/<name>//g'\
        -e 's/<\/name>/,/g'\
        -e 's/<value>//g'\
        -e 's/<\/value>/./g'\
        -e 's/<\/entry>//g'     asd

} >aaa.txt

输出如下:

Baneling,
ARMOR,
0.

Baneling,
BUILD_TIME,
20.

Baneling,
GAS,
25.

Baneling,
HP,
30.

计划使用 awk,因此我使用分隔符“.”。对于新行和“,”,因此文件看起来像 csv。

我考虑过的方法是使用

切-c 24-524

只取第三列,其中 XML 格式并将其返回,这样我就可以使用之前的数字,但自己无法做到这一点。

欢迎任何想法或解决方案。

答案1

xml2

<yourfile grep '^BDY' |
  cut -b24- |
  xml2 |
  2csv record/entry @unit name value

可以带你走一些路。

答案2

使用,这为您提供了 xml“ID”作为记录号,但不是 xml“段”,在我看来,这不是特别有用(它只是 xml 的切碎子字符串的数字)。

awk '
  BEGIN {
    print "<records>"                    # start a root-level tag
  }
  $1 == "BDY" {printf "%s", substr($0,24)}
  END {
    printf "\n"
    print "</value></entry></record>"    # because the sample data is incomplete, close the open tags
    print "</records>"                   # close the top-level tag
  }
' file \
| xmlstarlet sel -t \
    -m //record --var n='position()' \
    -m //entry -v '$n' -o ','  -v '@unit' -o ',' -v ./name -o ',' -v ./value -n

这不考虑任何可能包含逗号或文字双引号的值:生成的 CSV可能无效。

输出:

1,Baneling,ARMOR,0
1,Baneling,BUILD_TIME,20
1,Baneling,GAS,25
1,Baneling,HP,30
1,Baneling,MINERAL,75
1,Baneling,POP,.50
1,Baneling,SPEED,2.50
1,Baneling,TYPE,-
1,Baneling,UPG_SPEED,2.95
1,Brood Lord,ARMOR,1
1,Brood Lord,BUILD_TIME,34
1,Brood Lord,GAS,250
1,Brood Lord,HP,225
1,Brood Lord,MINERAL,300
1,Brood Lord,POP,4.00
1,Brood Lord,SPEED,1.41
1,Brood Lord,TYPE,Armored / Massive
1,Brood Lord,UPG_SPEED,1.41
1,Broodling,ARMOR,0
1,Broodling,BUILD_TIME,1
1,Broodling,GAS,0
1,Broodling,HP,30
1,Broodling,MINERAL,0
1,Broodling,POP,.00
1,Broodling,SPEED,3.84
1,Broodling,TYPE,Light
1,Broodling,UPG_SPEED,3.84
1,Changeling,ARMOR,0
1,Changeling,BUILD_TIME,1
1,Changeling,GAS,0
1,Changeling,HP,5
1,Changeling,MINERAL,0
1,Changeling,POP,.00
1,Changeling,SPEED,2.25
1,Changeling,TYPE,Light
1,Changeling,UPG_SPEED,2.25
1,Corruptor,ARMOR,2
1,Corruptor,BUILD_TIME,40
1,Corruptor,GAS,100
1,Corruptor,HP,200
1,Corruptor,MINERAL,150
1,Corruptor,POP,2.00
1,Corruptor,SPEED,2.95
1,Corruptor,TYPE,Armored
1,Corruptor,UPG_SPEED,2.95
1,Drone,ARMOR,0
1,Drone,BUILD_TIME,17
1,Drone,GAS,0
1,Drone,HP,40
1,Drone,MINERAL,50
1,Drone,POP,1.00
1,Drone,SPEED,2.81
1,Drone,TYPE,Light
1,Drone,UPG_SPEED,2.81
1,Hydralisk,ARMOR,0
1,Hydralisk,BUILD_TIME,33
1,Hydralisk,GAS,50
1,Hydralisk,HP,80
1,Hydralisk,MINERAL,100
1,Hydralisk,POP,2.00
1,Hydralisk,SPEED,2.25
1,Hydralisk,TYPE,Light
1,Hydralisk,UPG_SPEED,2.25
1,Infested Terran,ARMOR,0
1,Infested Terran,BUILD_TIME,50
1,Infested Terran,GAS,0
1,Infested Terran,HP,50
1,Infested Terran,MINERAL,0
1,Infested Terran,POP,.00
1,Infested Terran,SPEED,1.50
1,Infested Terran,TYPE,-
1,Infested Terran,UPG_SPEED,1.50
1,Infestor,ARMOR,0
1,Infestor,BUILD_TIME,50
1,Infestor,GAS,150
1,Infestor,HP,90
1,Infestor,MINERAL,100
1,Infestor,POP,2.00
1,Infestor,SPEED,2.25
1,Infestor,TYPE,Armored
1,Infestor,UPG_SPEED,2.25
1,Locust,ARMOR,0
1,Locust,BUILD_TIME,5
1,Locust,GAS,0
1,Locust,HP,65
1,Locust,MINERAL,0
1,Locust,POP,.00
1,Locust,SPEED,1.88
1,Locust,TYPE,Light
1,Locust,UPG_SPEED,1.88
1,Mutalisk,ARMOR,0
1,Mutalisk,BUILD_TIME,33
1,Mutalisk,GAS,100
1,Mutalisk,HP,120
1,Mutalisk,MINERAL,100
1,Mutalisk,POP,2.00
1,Mutalisk,SPEED,3.75
1,Mutalisk,TYPE,Light
1,Mutalisk,UPG_SPEED,3.75
1,Overlord,ARMOR,0
1,Overlord,BUILD_TIME,25
1,Overlord,GAS,0
1,Overlord,HP,200
1,Overlord,MINERAL,100
1,Overlord,POP,.00
1,Overlord,SPEED,.59
1,Overlord,TYPE,Armored
1,Overlord,UPG_SPEED,1.88
1,Overseer,ARMOR,0
1,Overseer,BUILD_TIME,17
1,Overseer,GAS,50
1,Overseer,HP,200
1,Overseer,MINERAL,150
1,Overseer,POP,.00
1,Overseer,SPEED,1.88
1,Overseer,TYPE,Armored
1,Overseer,UPG_SPEED,2.75
1,Queen,ARMOR,1
1,Queen,BUILD_TIME,50
1,Queen,GAS,0
1,Queen,HP,175
1,Queen,MINERAL,150
1,Queen,POP,2.00
1,Queen,SPEED,.94
1,Queen,TYPE,-
1,Queen,UPG_SPEED,.94
1,Roach,ARMOR,1
1,Roach,BUILD_TIME,27
1,Roach,GAS,25
1,Roach,HP,145
1,Roach,MINERAL,75
1,Roach,POP,2.00
1,Roach,SPEED,2.25
1,Roach,TYPE,Armored
1,Roach,UPG_SPEED,3.00
1,Spine Crawler,ARMOR,2
1,Spine Crawler,BUILD_TIME,50
1,Spine Crawler,GAS,0
1,Spine Crawler,HP,300
1,Spine Crawler,MINERAL,150
1,Spine Crawler,POP,.00
1,Spine Crawler,SPEED,1.00
1,Spine Crawler,TYPE,Armored / Structure
1,Spine Crawler,UPG_SPEED,1.00
1,Spore Crawler,ARMOR,1
1,Spore Crawler,BUILD_TIME,30
1,Spore Crawler,GAS,0
1,Spore Crawler,HP,400
1,Spore Crawler,MINERAL,150
1,Spore Crawler,POP,.00
1,Spore Crawler,SPEED,1.00
1,Spore Crawler,TYPE,Armored / Structure
1,Spore Crawler,UPG_SPEED,1.00
1,Swarm Host,ARMOR,1
1,Swarm Host,BUILD_TIME,40
1,Swarm Host,GAS,100
1,Swarm Host,HP,160
1,Swarm Host,MINERAL,200
1,Swarm Host,POP,3.00
1,Swarm Host,SPEED,2.25
1,Swarm Host,TYPE,Armored
1,Swarm Host,UPG_SPEED,2.25
1,Ultralisk,ARMOR,1
1,Ultralisk,BUILD_TIME,55
1,Ultralisk,GAS,200
1,Ultralisk,HP,500
1,Ultralisk,MINERAL,300
1,Ultralisk,POP,6.00
1,Ultralisk,SPEED,2.95
1,Ultralisk,TYPE,Armored / Massive
1,Ultralisk,UPG_SPEED,2.95
1,Viper,ARMOR,1
1,Viper,BUILD_TIME,40
1,Viper,GAS,200
1,Viper,HP,150
1,Viper,MINERAL,100
1,Viper,POP,3.00
1,Viper,SPEED,2.95
1,Viper,TYPE,Armored
1,Viper,UPG_SPEED,2.95
1,Zergling,ARMOR,0
1,Zergling,BUILD_TIME,24
1,Zergling,GAS,0
1,Zergling,HP,35
1,Zergling,MINERAL,25
1,Zergling,POP,.50
1,Zergling,SPEED,2.95
1,Zergling,TYPE,Light
1,Zergling,UPG_SPEED,4.70
1,Archon,ARMOR,0
1,Archon,BUILD_TIME,12
1,Archon,GAS,300
1,Archon,HP,10
1,Archon,MINERAL,
1,Archon,POP,4.00
1,Archon,SHIELD,350
1,Archon,SPEED,2.81
1,Archon,TOTAL,360
1,Archon,TYPE,Massive
1,Archon,UPG_SPEED,2.81
1,Carrier (4 interc.),ARMOR,2
1,Carrier (4 interc.),BUILD_TIME,120
1,Carrier (4 interc.),GAS,250
1,Carrier (4 interc.),HP,300
1,Carrier (4 interc.),MINERAL,
1,Carrier (4 interc.),POP,6.00
1,Carrier (4 interc.),SHIELD,150
1,Carrier (4 interc.),SPEED,1.88
1,Carrier (4 interc.),TOTAL,450
1,Carrier (4 interc.),TYPE,Armored / Massive
1,Carrier (4 interc.),UPG_SPEED,1.88
1,Colossus,ARMOR,1
1,Colossus,BUILD_TIME,75
1,Colossus,GAS,200
1,Colossus,HP,200
1,Colossus,MINERAL,
1,Colossus,POP,6.00
1,Colossus,SHIELD,150
1,Colossus,SPEED,2.25
1,Colossus,TOTAL,350
1,Colossus,TYPE,Armored / Massive
1,Colossus,UPG_SPEED,2.25
1,Dark Templar,ARMOR,1
1,Dark Templar,BUILD_TIME,55
1,Dark Templar,GAS,125
1,Dark Templar,HP,40
1,Dark Templar,MINERAL,
1,Dark Templar,POP,2.00
1,Dark Templar,SHIELD,80
1,Dark Templar,SPEED,2.81
1,Dark Templar,TOTAL,120
1,Dark Templar,TYPE,-
1,Dark Templar,UPG_SPEED,2.81
1,High Templar,ARMOR,0
1,High Templar,BUILD_TIME,55
1,High Templar,GAS,150
1,High Templar,HP,40
1,High Templar,MINERAL,
1,High Templar,POP,2.00
1,High Templar,SHIELD,40
1,High Templar,SPEED,1.88
1,High Templar,TOTAL,80
1,High Templar,TYPE,-
1,High Templar,UPG_SPEED,1.88
1,Immortal,ARMOR,1
1,Immortal,BUILD_TIME,50
1,Immortal,GAS,100
1,Immortal,HP,200
1,Immortal,MINERAL,
1,Immortal,POP,4.00
1,Immortal,SHIELD,100
1,Immortal,SPEED,2.25
1,Immortal,TOTAL,300
1,Immortal,TYPE,Armored
1,Immortal,UPG_SPEED,2.25
1,Interceptor,ARMOR,0
1,Interceptor,BUILD_TIME,8
1,Interceptor,GAS,0
1,Interceptor,HP,40
1,Interceptor,MINERAL,
1,Interceptor,POP,.00
1,Interceptor,SHIELD,40
1,Interceptor,SPEED,7.50
1,Interceptor,TOTAL,80
1,Interceptor,TYPE,Light
1,Interceptor,UPG_SPEED,7.50
1,Mothership,ARMOR,2
1,Mothership,BUILD_TIME,160
1,Mothership,GAS,400
1,Mothership,HP,350
1,Mothership,MINERAL,
1,Mothership,POP,8.00
1,Mothership,SHIELD,350
1,Mothership,SPEED,1.41
1,Mothership,TOTAL,700
1,Mothership,TYPE,Massive
1,Mothership,UPG_SPEED,1.41
1,Mothership Core,ARMOR,1
1,Mothership Core,BUILD_TIME,30
1,Mothership Core,GAS,100
1,Mothership Core,HP,130
1,Mothership Core,MINERAL,
1,Mothership Core,POP,2.00
1,Mothership Core,SHIELD,60
1,Mothership Core,SPEED,1.88
1,Mothership Core,TOTAL,190
1,Mothership Core,TYPE,Armored
1,Mothership Core,UPG_SPEED,1.88
1,Observer,ARMOR,0
1,Observer,BUILD_TIME,30
1,Observer,GAS,75
1,Observer,HP,40
1,Observer,MINERAL,
1,Observer,POP,1.00
1,Observer,SHIELD,20
1,Observer,SPEED,1.88
1,Observer,TOTAL,60
1,Observer,TYPE,Light
1,Observer,UPG_SPEED,2.81
1,Oracle,ARMOR,0
1,Oracle,BUILD_TIME,50
1,Oracle,GAS,150
1,Oracle,HP,100
1,Oracle,MINERAL,
1,Oracle,POP,3.00
1,Oracle,SHIELD,60
1,Oracle,SPEED,3.38
1,Oracle,TOTAL,160
1,Oracle,TYPE,Light
1,Oracle,UPG_SPEED,3.38
1,Phoenix,ARMOR,0

2,Baneling,ARMOR,0
2,Baneling,BUILD_TIME,20
2,Baneling,GAS,25
2,Baneling,HP,30
2,Baneling,MINERAL,75
2,Baneling,POP,.50
2,Baneling,SPEED,2.50
2,Baneling,TYPE,-
2,Baneling,UPG_SPEED,2.95
2,Brood Lord,ARMOR,1
2,Brood Lord,BUILD_TIME,34
2,Brood Lord,GAS,250
2,Brood Lord,HP,225
2,Brood Lord,MINERAL,300
2,Brood Lord,POP,4.00
2,Brood Lord,SPEED,1.41
2,Brood Lord,TYPE,Armored / Massive
2,Brood Lord,UPG_SPEED,1.41
2,Broodling,ARMOR,0
2,Broodling,BUILD_TIME,1
2,Broodling,GAS,0
2,Broodling,HP,30
2,Broodling,MINERAL,0
2,Broodling,POP,.00
2,Broodling,SPEED,3.84
2,Broodling,TYPE,Light
2,Broodling,UPG_SPEED,3.84
2,Changeling,ARMOR,0
2,Changeling,BUILD_TIME,1
2,Changeling,GAS,0
2,Changeling,HP,5
2,Changeling,MINERAL,0
2,Changeling,POP,.00
2,Changeling,SPEED,2.25
2,Changeling,TYPE,Light
2,Changeling,UPG_SPEED,2.25
2,Corruptor,ARMOR,2
2,Corruptor,BUILD_TIME,40
2,Corruptor,GAS,100
2,Corruptor,HP,200
2,Corruptor,MINERAL,150
2,Corruptor,POP,2.00
2,Corruptor,SPEED,2.95
2,Corruptor,TYPE,Armored
2,Corruptor,UPG_SPEED,2.95
2,Drone,ARMOR,0
2,Drone,BUILD_TIME,17
2,Drone,GAS,0
2,Drone,HP,40
2,Drone,MINERAL,50
2,Drone,POP,1.00
2,Drone,SPEED,2.81
2,Drone,TYPE,Light
2,Drone,UPG_SPEED,2.81
2,Hydralisk,ARMOR,0
2,Hydralisk,BUILD_TIME,33
2,Hydralisk,GAS,50
2,Hydralisk,HP,80
2,Hydralisk,MINERAL,100
2,Hydralisk,POP,2.00
2,Hydralisk,SPEED,2.25
2,Hydralisk,TYPE,Light
2,Hydralisk,UPG_SPEED,2.25
2,Infested Terran,ARMOR,0
2,Infested Terran,BUILD_TIME,50
2,Infested Terran,GAS,0
2,Infested Terran,HP,50
2,Infested Terran,MINERAL,0
2,Infested Terran,POP,.00
2,Infested Terran,SPEED,1.50
2,Infested Terran,TYPE,-
2,Infested Terran,UPG_SPEED,1.50
2,Infestor,ARMOR,0
2,Infestor,BUILD_TIME,50
2,Infestor,GAS,150
2,Infestor,HP,90
2,Infestor,MINERAL,100
2,Infestor,POP,2.00
2,Infestor,SPEED,2.25
2,Infestor,TYPE,Armored
2,Infestor,UPG_SPEED,2.25
2,Locust,ARMOR,0
2,Locust,BUILD_TIME,5
2,Locust,GAS,0
2,Locust,HP,65
2,Locust,MINERAL,0
2,Locust,POP,.00
2,Locust,SPEED,1.88
2,Locust,TYPE,Light
2,Locust,UPG_SPEED,1.88
2,Mutalisk,ARMOR,0
2,Mutalisk,BUILD_TIME,33
2,Mutalisk,GAS,100
2,Mutalisk,HP,120
2,Mutalisk,MINERAL,100
2,Mutalisk,POP,2.00
2,Mutalisk,SPEED,3.75
2,Mutalisk,TYPE,Light
2,Mutalisk,UPG_SPEED,3.75
2,Overlord,ARMOR,0
2,Overlord,BUILD_TIME,25
2,Overlord,GAS,0
2,Overlord,HP,200
2,Overlord,MINERAL,100
2,Overlord,POP,.00
2,Overlord,SPEED,.59
2,Overlord,TYPE,Armored
2,Overlord,UPG_SPEED,1.88
2,Overseer,ARMOR,0
2,Overseer,BUILD_TIME,17
2,Overseer,GAS,50
2,Overseer,HP,200
2,Overseer,MINERAL,150
2,Overseer,POP,.00
2,Overseer,SPEED,1.88
2,Overseer,TYPE,Armored
2,Overseer,UPG_SPEED,2.75
2,Queen,ARMOR,1
2,Queen,BUILD_TIME,50
2,Queen,GAS,0
2,Queen,HP,175
2,Queen,MINERAL,150
2,Queen,POP,2.00
2,Queen,SPEED,.94
2,Queen,TYPE,-
2,Queen,UPG_SPEED,.94
2,Roach,ARMOR,1
2,Roach,BUILD_TIME,27
2,Roach,GAS,25
2,Roach,HP,145
2,Roach,MINERAL,75
2,Roach,POP,2.00
2,Roach,SPEED,2.25
2,Roach,TYPE,Armored
2,Roach,UPG_SPEED,3.00
2,Spine Crawler,ARMOR,2
2,Spine Crawler,BUILD_TIME,50
2,Spine Crawler,GAS,0
2,Spine Crawler,HP,300
2,Spine Crawler,MINERAL,150
2,Spine Crawler,POP,.00
2,Spine Crawler,SPEED,1.00
2,Spine Crawler,TYPE,Armored / Structure
2,Spine Crawler,UPG_SPEED,1.00
2,Spore Crawler,ARMOR,1
2,Spore Crawler,BUILD_TIME,30
2,Spore Crawler,GAS,0
2,Spore Crawler,HP,400
2,Spore Crawler,MINERAL,150
2,Spore Crawler,POP,.00
2,Spore Crawler,SPEED,1.00
2,Spore Crawler,TYPE,Armored / Structure
2,Spore Crawler,UPG_SPEED,1.00
2,Swarm Host,ARMOR,1
2,Swarm Host,BUILD_TIME,40
2,Swarm Host,GAS,100
2,Swarm Host,HP,160
2,Swarm Host,MINERAL,200
2,Swarm Host,POP,3.00
2,Swarm Host,SPEED,2.25
2,Swarm Host,TYPE,Armored
2,Swarm Host,UPG_SPEED,2.25
2,Ultralisk,ARMOR,1
2,Ultralisk,BUILD_TIME,55
2,Ultralisk,GAS,200
2,Ultralisk,HP,500
2,Ultralisk,MINERAL,300
2,Ultralisk,POP,6.00
2,Ultralisk,SPEED,2.95
2,Ultralisk,TYPE,Armored / Massive
2,Ultralisk,UPG_SPEED,2.95
2,Viper,ARMOR,1
2,Viper,BUILD_TIME,40
2,Viper,GAS,200
2,Viper,HP,150
2,Viper,MINERAL,100
2,Viper,POP,3.00
2,Viper,SPEED,2.95
2,Viper,TYPE,Armored
2,Viper,UPG_SPEED,2.95
2,Zergling,ARMOR,0
2,Zergling,BUILD_TIME,24
2,Zergling,GAS,0
2,Zergling,HP,35
2,Zergling,MINERAL,25
2,Zergling,POP,.50
2,Zergling,SPEED,2.95
2,Zergling,TYPE,Light
2,Zergling,UPG_SPEED,4.70
2,Archon,ARMOR,0
2,Archon,BUILD_TIME,12
2,Archon,GAS,300
2,Archon,HP,10
2,Archon,MINERAL,
2,Archon,POP,4.00
2,Archon,SHIELD,350
2,Archon,SPEED,2.81
2,Archon,TOTAL,360
2,Archon,TYPE,Massive
2,Archon,UPG_SPEED,2.81
2,Carrier (4 interc.),ARMOR,2
2,Carrier (4 interc.),BUILD_TIME,120
2,Carrier (4 interc.),GAS,250
2,Carrier (4 interc.),HP,300
2,Carrier (4 interc.),MINERAL,
2,Carrier (4 interc.),POP,6.00
2,Carrier (4 interc.),SHIELD,150
2,Carrier (4 interc.),SPEED,1.88
2,Carrier (4 interc.),TOTAL,450
2,Carrier (4 interc.),TYPE,Armored / Massive
2,Carrier (4 interc.),UPG_SPEED,1.88
2,Colossus,ARMOR,1
2,Colossus,BUILD_TIME,75
2,Colossus,GAS,200
2,Colossus,HP,200
2,Colossus,MINERAL,
2,Colossus,POP,6.00
2,Colossus,SHIELD,150
2,Colossus,SPEED,2.25
2,Colossus,TOTAL,350
2,Colossus,TYPE,Armored / Massive
2,Colossus,UPG_SPEED,2.25
2,Dark Templar,ARMOR,1
2,Dark Templar,BUILD_TIME,55
2,Dark Templar,GAS,125
2,Dark Templar,HP,40
2,Dark Templar,MINERAL,
2,Dark Templar,POP,2.00
2,Dark Templar,SHIELD,80
2,Dark Templar,SPEED,2.81
2,Dark Templar,TOTAL,120
2,Dark Templar,TYPE,-
2,Dark Templar,UPG_SPEED,2.81
2,High Templar,ARMOR,0
2,High Templar,BUILD_TIME,55
2,High Templar,GAS,150
2,High Templar,HP,40
2,High Templar,MINERAL,
2,High Templar,POP,2.00
2,High Templar,SHIELD,40
2,High Templar,SPEED,1.88
2,High Templar,TOTAL,80
2,High Templar,TYPE,-
2,High Templar,UPG_SPEED,1.88
2,Immortal,ARMOR,1
2,Immortal,BUILD_TIME,50
2,Immortal,GAS,100
2,Immortal,HP,200
2,Immortal,MINERAL,
2,Immortal,POP,4.00
2,Immortal,SHIELD,100
2,Immortal,SPEED,2.25
2,Immortal,TOTAL,300
2,Immortal,TYPE,Armored
2,Immortal,UPG_SPEED,2.25
2,Interceptor,ARMOR,0
2,Interceptor,BUILD_TIME,8
2,Interceptor,GAS,0
2,Interceptor,HP,40
2,Interceptor,MINERAL,
2,Interceptor,POP,.00
2,Interceptor,SHIELD,40
2,Interceptor,SPEED,7.50
2,Interceptor,TOTAL,80
2,Interceptor,TYPE,Light
2,Interceptor,UPG_SPEED,7.50
2,Mothership,ARMOR,2
2,Mothership,BUILD_TIME,160
2,Mothership,GAS,400
2,Mothership,HP,350
2,Mothership,MINERAL,
2,Mothership,POP,8.00
2,Mothership,SHIELD,350
2,Mothership,SPEED,1.41
2,Mothership,TOTAL,700
2,Mothership,TYPE,Massive
2,Mothership,UPG_SPEED,1.41
2,Mothership Core,ARMOR,1
2,Mothership Core,BUILD_TIME,30
2,Mothership Core,GAS,100
2,Mothership Core,HP,130
2,Mothership Core,MINERAL,
2,Mothership Core,POP,2.00
2,Mothership Core,SHIELD,60
2,Mothership Core,SPEED,1.88
2,Mothership Core,TOTAL,190
2,Mothership Core,TYPE,Armored
2,Mothership Core,UPG_SPEED,1.88
2,Observer,ARMOR,0
2,Observer,BUILD_TIME,30
2,Observer,GAS,75
2,Observer,HP,40
2,Observer,MINERAL,
2,Observer,POP,1.00
2,Observer,SHIELD,20
2,Observer,SPEED,1.88
2,Observer,TOTAL,60
2,Observer,TYPE,Light
2,Observer,UPG_SPEED,2.81
2,Oracle,ARMOR,0
2,Oracle,BUILD_TIME,50
2,Oracle,GAS,150
2,Oracle,HP,100
2,Oracle,MINERAL,
2,Oracle,POP,3.00
2,Oracle,SHIELD,60
2,Oracle,SPEED,3.38
2,Oracle,TOTAL,160
2,Oracle,TYPE,Light
2,Oracle,UPG_SPEED,3.38
2,Phoenix,ARMOR,0

答案3

使用任何 POSIX awk 并假设您的数据值不包含任何标签<entry unit="..."><name><value>或(它可以变得更健壮,但永远不会 100% 健壮,我希望这可能足以满足您的实际需求)</name></value>

$ cat tst.sh
#!/usr/bin/env bash

awk '
    BEGIN { OFS="," }
    NR == 1 { next }
    $2 != id {
        if ( id != "" ) {
            prtRec()
        }
        id = $2
        rec = ""
    }
    {
        # Strip the leading irrelevant chars and join
        # all the lines in each record.
        sub(/([^[:space:]]+[[:space:]]+){2}/,"")
        rec = rec $0
    }
    END {
        prtRec()
    }

    function prtRec(    entry, numSegs, unit) {
        # format and print the current record

        if ( !doneHdr++ ) {
            print "ID_XML", "ID_SEGMENT", "UNIT", "NAME", "VALUE"
        }

        # Strip all leading and/or trailing spaces from the record
        gsub(/^[[:space:]]+|[[:space:]]+$/,"",rec)

        # Replace these tag ends with newline chars, which cannot be
        # present in the record, so we can negate them in the regexp
        gsub("</(name|value)>","\n",rec)

        while ( match(rec,/<entry unit="[^"]*"><name>[^\n]*\n<value>[^\n]*\n<\/entry>/) ) {
            entry = substr(rec,RSTART,RLENGTH)

            split(entry,parts,/<entry unit="|"><name>|<value>|\n/)

            print id, ++numSegs, parts[2], parts[3], parts[5]

            rec = substr(rec,RSTART+RLENGTH)
        }
    }
'  "${@:--}"

$ ./tst.sh file
ID_XML,ID_SEGMENT,UNIT,NAME,VALUE
1,1,Baneling,ARMOR,0
1,2,Baneling,BUILD_TIME,20
1,3,Baneling,GAS,25
1,4,Baneling,HP,30
1,5,Baneling,MINERAL,75
1,6,Baneling,POP,.50
1,7,Baneling,SPEED,2.50
1,8,Baneling,UPG_SPEED,2.95
1,9,Brood Lord,ARMOR,1
1,10,Brood Lord,BUILD_TIME,34
1,11,Brood Lord,GAS,250
1,12,Brood Lord,HP,225
1,13,Brood Lord,MINERAL,300
1,14,Brood Lord,POP,34.00
1,15,Brood Lord,SPEED,1.41
1,16,Brood Lord,TYPE,Armored / Massive
1,17,Brood Lord,UPG_SPEED,1.41
1,18,Broodling,ARMOR,0
1,19,Broodling,BUILD_TIME,1
1,20,Broodling,GAS,0
1,21,Broodling,MINERAL,0
1,22,Broodling,POP,.00
1,23,Broodling,SPEED,3.84
1,24,Broodling,TYPE,Light
1,25,Broodling,UPG_SPEED,3.84
1,26,Changeling,ARMOR,0
1,27,Changeling,BUILD_TIME,1
1,28,Changeling,HP,5
1,29,Changeling,MINERAL,0
1,30,Changeling,POP,.00
1,31,Changeling,SPEED,2.25
1,32,Changeling,TYPE,Light
1,33,Changeling,UPG_SPEED,2.25
1,34,Corruptor,BUILD_TIME,40
1,35,Corruptor,GAS,100
1,36,Corruptor,HP,200
1,37,Corruptor,MINERAL,150
1,38,Corruptor,POP,2.00
1,39,Corruptor,SPEED,2.95
1,40,Corruptor,TYPE,Arm7ored
1,41,Corruptor,UPG_SPEED,2.95
1,42,Drone,ARMOR,0
1,43,Drone,BUILD_TIME,17
1,44,Drone,GAS,0
1,45,Drone,HP,40
1,46,Drone,MINERAL,50
1,47,Drone,POP,1.00
1,48,Drone,SPE8ED,2.81
1,49,Drone,TYPE,Light
1,50,Drone,UPG_SPEED,2.81
1,51,Hydralisk,ARMOR,0
1,52,Hydralisk,BUILD_TIME,33
1,53,Hydralisk,GAS,50
1,54,Hydralisk,HP,80
1,55,Hydralisk,POP,2.00
1,56,Hydralisk,SPEED,2.25
1,57,Hydralisk,TYPE,Light
1,58,Hydralisk,UPG_SPEED,2.25
1,59,Infested Terran,ARMOR,0
1,60,Infested Terran,BUILD_TIME,50
1,61,Infested Terran,HP,50
1,62,Infested Terran,MINERAL,0
1,63,Infested Terran,POP,.00
1,64,Infested Terran,SPEED,1.50
1,65,Infested Terran,TYPE,-
1,66,Infested Terran,UPG_SPEED,1.50
1,67,Infestor,BUILD_TIME,50
1,68,Infestor,GAS,150
1,69,Infestor,HP,90
1,70,Infestor,MINERAL,100
1,71,Infestor,POP,2.00
1,72,Infestor,SPEED,2.25
1,73,Infestor,TYPE,Armored
1,74,Infe12stor,UPG_SPEED,2.25
1,75,Locust,ARMOR,0
1,76,Locust,BUILD_TIME,5
1,77,Locust,GAS,0
1,78,Locust,HP,65
1,79,Locust,MINERAL,0
1,80,Locust,POP,.00
1,81,Locust,TYPE,Light
1,82,Locust,UPG_SPEED,1.88
1,83,Mutalisk,ARMOR,0
1,84,Mutalisk,BUILD_TIME,33
1,85,Mutalisk,GAS,100
1,86,Mutalisk,HP,120
1,87,Mutalisk,MINERAL,100
1,88,Mutalisk,SPEED,3.75
1,89,Mutalisk,TYPE,Light
1,90,Mutalisk,UPG_SPEED,3.75
1,91,Overlord,ARMOR,0
1,92,Overlord,BUILD_TIME,25
1,93,Overlord,GAS,0
1,94,Overlord,MINERAL,100
1,95,Overlord,POP,.00
1,96,Overlord,SPEED,.59
1,97,Overlord,TYPE,Armored
1,98,Overlord,UPG_SPEED,1.88
1,99,Overseer,ARMOR,0
1,100,Overseer,BUILD_TIME,17
1,101,Overseer,HP,200
1,102,Overseer,MINERAL,150
1,103,Overseer,POP,.00
1,104,Overseer,SPEED,1.88
1,105,Overseer,TYPE,Armored
1,106,Overseer,UPG_SPEED,2.75
1,107,Queen,BUILD_TIME,50
1,108,Queen,GAS,0
1,109,Queen,HP,175
1,110,Queen,MINERAL,150
1,111,Queen,POP,2.00
1,112,Queen,SPEED,.94
1,113,Queen,TYPE,-
1,114,Queen,UPG_SPEED18,.94
1,115,Roach,ARMOR,1
1,116,Roach,BUILD_TIME,27
1,117,Roach,GAS,25
1,118,Roach,HP,145
1,119,Roach,MINERAL,75
1,120,Roach,POP,2.00
1,121,Roach,SPEED,2.25
1,122,Roach,UPG_SPEED,3.00
1,123,Spine Crawler,ARMOR,2
1,124,Spine Crawler,BUILD_TIME,50
1,125,Spine Crawler,GAS,0
1,126,Spine Crawler,HP,300
1,127,Spine Crawler,MINERAL,150
1,128,Spine Crawler,SPEED,1.00
1,129,Spine Crawler,TYPE,Armored / Structure
1,130,Spine Crawler,UPG_SPEED,1.00
1,131,Spore Crawler,ARMOR,1
1,132,Spore Crawler,BUILD_TIME,30
1,133,Spore Crawler,GAS,0
1,134,Spore Crawler,MINERAL,150
1,135,Spore Crawler,POP,.00
1,136,Spore Crawler,SPEED,1.00
1,137,Spore Crawler,TYPE,Armored / Structure
1,138,Spore Crawler,UPG_SPEED,1.00
1,139,Swarm Host,BUILD_TIME,40
1,140,Swarm Host,GAS,100
1,141,Swarm Host,HP,160
1,142,Swarm Host,MINERAL,200
1,143,Swarm Host,POP,3.00
1,144,Swarm Host,SPEED,2.25
1,145,Swarm Host,TYPE,Armored
1,146,Ultralisk,ARMOR,1
1,147,Ultralisk,BUILD_TIME,55
1,148,Ultralisk,GAS,200
1,149,Ultralisk,HP,500
1,150,Ultralisk,MINERAL,300
1,151,Ultralisk,POP,6.00
1,152,Ultralisk,TYPE,Armored / Massive
1,153,Ultralisk,UPG_SPEED,2.95
1,154,Viper,ARMOR,1
1,155,Viper,BUILD_TIME,40
1,156,Viper,GAS,200
1,157,Viper,HP,150
1,158,Viper,MINERAL,12500
1,159,Viper,POP,3.00
1,160,Viper,SPEED,2.95
1,161,Viper,TYPE,Armored
1,162,Viper,UPG_SPEED,2.95
1,163,Zergling,ARMOR,0
1,164,Zergling,BUILD_TIME,24
1,165,Zergling,GAS,0
1,166,Zergli26ng,HP,35
1,167,Zergling,MINERAL,25
1,168,Zergling,POP,.50
1,169,Zergling,SPEED,2.95
1,170,Zergling,TYPE,Light
1,171,Zergling,UPG_SPEED,4.70
2,1,Archon,ARMOR,0
2,2,Archon,BUILD_TIME,12
2,3,Archon,GAS,300
2,4,Archon,HP,10
2,5,Archon,MINERAL,
2,6,Archon,POP,4.00
2,7,Archon,SHIELD,350
2,8,Archon,TOTAL,360
2,9,Archon,TYPE,Massive
2,10,Archon,UPG_SPEED,2.81
2,11,Carrier (4 interc.),ARMOR,2
2,12,Carrier (4 interc.),BUILD_TIME,120
2,13,Carrier (4 interc.),GAS,250
2,14,Carrier (4 interc.),MINERAL,
2,15,Carrier (4 interc.),POP,6.00
2,16,Carrier (4 interc.),SHIELD,150
2,17,Carrier (4 interc.),SPEED,1.88
2,18,Carrier (4 interc.),TOTAL,450
2,19,Carrier (4 interc.),TYPE,Armored / Massi4ve
2,20,Carrier (4 interc.),UPG_SPEED,1.88
2,21,Colossus,ARMOR,1
2,22,Colossus,BUILD_TIME,75
2,23,Colossus,GAS,200
2,24,Colossus,HP,200
2,25,Colossus,MINERAL,
2,26,Colossus,POP,6.00
2,27,Colossus,SPEED,2.25
2,28,Colossus,TOTAL,350
2,29,Colossus,TYPE,Armored / Massive
2,30,Colossus,UPG_SPEED,2.25
2,31,Dark Templar,ARMOR,1
2,32,Dark Templar,BUILD_TIME,55
2,33,Dark Templar,HP,40
2,34,Dark Templar,MINERAL,
2,35,Dark Templar,POP,2.00
2,36,Dark Templar,SHIELD,80
2,37,Dark Templar,SPEED,2.81
2,38,Dark Templar,TOTAL,120
2,39,7Dark Templar,TYPE,-
2,40,Dark Templar,UPG_SPEED,2.81
2,41,High Templar,ARMOR,0
2,42,High Templar,BUILD_TIME,55
2,43,High Templar,GAS,150
2,44,High Templar,HP,40
2,45,High Templar,MINERAL,
2,46,High T8emplar,POP,2.00
2,47,High Templar,SHIELD,40
2,48,High Templar,SPEED,1.88
2,49,High Templar,TOTAL,80
2,50,High Templar,TYPE,-
2,51,High Templar,UPG_SPEED,1.88
2,52,Immortal,ARMOR,1
2,53,Immortal,GAS,100
2,54,Immortal,HP,200
2,55,Immortal,MINERAL,
2,56,Immortal,POP,4.00
2,57,Immortal,SHIELD,100
2,58,Immortal,SPEED,2.25
2,59,Immortal,TYPE,Armored
2,60,Immortal,UPG_SPEED,2.25
2,61,Interceptor,ARMOR,0
2,62,Interceptor,BUILD_TIME,8
2,63,Interceptor,GAS,0
2,64,Interceptor,HP,40
2,65,Interceptor,MINERAL,
2,66,Interceptor,SHIELD,40
2,67,Interceptor,SPEED,7.50
2,68,Interceptor,TOTAL,80
2,69,Interceptor,TYPE,Light
2,70,Interceptor,UPG_SPEED,7.50
2,71,Mothership,ARMOR,2
2,72,Moth12ership,BUILD_TIME,160
2,73,Mothership,GAS,400
2,74,Mothership,HP,350
2,75,Mothership,MINERAL,
2,76,Mothership,POP,8.00
2,77,Mothership,SHIELD,350
2,78,Mothership,SPEED,1.41
2,79,Mothership,TOT13AL,700
2,80,Mothership,TYPE,Massive
2,81,Mothership,UPG_SPEED,1.41
2,82,Mothership Core,ARMOR,1
2,83,Mothership Core,BUILD_TIME,30
2,84,Mothership Core,GAS,100
2,85,Mothership Core,HP,130
2,86,Mothership Co14re,MINERAL,
2,87,Mothership Core,POP,2.00
2,88,Mothership Core,SHIELD,60
2,89,Mothership Core,SPEED,1.88
2,90,Mothership Core,TOTAL,190
2,91,Mothership Core,TYPE,Armored
2,92,Mothership Core,UPG_SPEED,1.88
2,93,Observer,BUILD_TIME,30
2,94,Observer,GAS,75
2,95,Observer,HP,40
2,96,Observer,MINERAL,
2,97,Observer,POP,1.00
2,98,Observer,SHIELD,20
2,99,Observer,TOTAL,60
2,100,Observer,TYPE,Light
2,101,Observer,UPG_SPEED,2.81
2,102,Oracle,ARMOR,0
2,103,Oracle,BUILD_TIME,50
2,104,Oracle,GAS,150
2,105,Oracle,HP,100
2,106,Oracle,POP,3.00
2,107,Oracle,SHIELD,60
2,108,Oracle,SPEED,3.38
2,109,Oracle,TOTAL,160
2,110,Oracle,TYPE,Light
2,111,Oracle,UPG_SPEED,3.38

相关内容