У меня есть файл фиксированной длины, как извлечь данные, чтобы они выглядели как CSV-файл

$ perl -F'<xxx>' -pale ' $"="<xxx>";$_ = "@F[1,3,5]"' file

Установите в качестве разделителя полей нужную строку, затем соберите индексы полей, соедините их строкой в ​​$" и сохраните как текущую запись. -p будет распечатан автоматически.

-1
17.11.2021, 14:28
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

0
17.11.2021, 16:06

Используя любой POSIX awk и предполагая, что ваши значения данных не содержат ни одного из тегов <entry unit="...">, <name>, <value>, </name>или </value>(, его можно сделать более надежным, но никогда не на 100% надежным. и я полагаю, что этого, вероятно, достаточно для ваших реальных нужд в любом случае):

$ 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
-1
17.11.2021, 22:23

Теги

Похожие вопросы