переформатирование текстового файла в формат CSV

Перенесено из вопроса в ответ:

Как указал h3rrmiller, я смог добиться этого с помощью rsyncс помощью опции --no-links.

0
05.12.2019, 15:36
2 ответа

Использованиеawk:

awk '{
  if (sub(/\.xml /, ".xml,")){      # replace `.xml ` with `.xml,`
    if (NR>1 && is_processed != 1){ # xml line was not printed?
       print xml","                 # print previous xml line + `,`
    }
    sub(/>?[0-9]+\.ABC$/, ",") # replace strings `>1446815.ABC` or `1244115.ABC` with `,`
    xml=$0                     # save line in variable `xml`
    is_processed=0             # clear flag
  }
  else {
    if (!NF) next  # skip empty line
    sub(/ /, ",")  # replace 1st ` ` with `,`
    sub(/ /, ",")  # replace 2nd ` ` with `,`
    print xml$0    # print xml line + current line
    is_processed=1 # set flag
  }
}
END {
  # print possible remaining line
  if (is_processed != 1) print xml","
}' filein > fileout

Блокif-обрабатывает строку, содержащую .xml, и сохраняет ее в переменной xml. Блокelse-обрабатывает следующие «потомки» строки xml и печатает строку xml плюс измененную строку, в которой первые два символа пробела заменены запятой. Пустые строки пропускаются.

Если "детей" нет, то строка xml с дополнительной запятой печатается в верхнемif-блоке (, если номер строки больше 1 )или в блоке END-.

Выход(fileout):

0bef-82-46-8a-9a0b.xml,"Fruits/Mango Apple /Plum cherry date",0bef-82-46-8a-9a0b,5da-0-ba-c1-1a9,"Fruits/Pear Banana/Plum orange mango"
0bef-82-46-8a-9a0b.xml,"Fruits/Mango Apple /Plum cherry date",0bef-82-46-8a-9a0b,ac-94-4ab-91-23,"Fruits/Pear Banana/Plum orange mango"
0bef-82-46-8a-9a0b.xml,"Fruits/Mango Apple /Plum cherry date",0bef-82-46-8a-9a0b,5z-94-ab-92-2f3,"Fruits/Pear Banana/Plum orange mango"
952f-82-46-8a-9a0b.xml,"Fruits/Mango",,
3cff-82-46-8a-9a0b.xml,"Fruits/Big Mango/Not Sweet ",,
6m0k-82-46-8a-9a0b.xml,"Fruits/Big Pear/Very Sweet",,
17a-42-df-c24.xml,"Fruits Market/Big Apple/Sweet ",17a-42-df-c24,54-ba-4411-9-3d8,"Veg/Radish /Radish Carrot Celery Onion"
17a-42-df-c24.xml,"Fruits Market/Big Apple/Sweet ",17a-42-df-c24,2da5-0-4a-b1-e89,"Veg/Radish /Radish Carrot Celery Onion"
17a-42-df-c24.xml,"Fruits Market/Big Apple/Sweet ",17a-42-df-c24,b7-94-4db-92-2f3,"Veg/Radish /Radish Carrot Celery Onion"
17a-42-df-c24.xml,"Fruits Market/Big Apple/Sweet ",17a-42-df-c24,4d-67c-446-b5-ac,"Veg/Radish /Radish Carrot Celery Onion"
17a-42-df-c24.xml,"Fruits Market/Big Apple/Sweet ",17a-42-df-c24,2-8b-4det-87-769,"Veg/Radish /Radish Carrot Celery Onion"
1
28.01.2020, 02:38

Использование Миллера(https://github.com/johnkerl/miller)и sed

<input.csv sed -r 's|^(.+")(.?[0-9]+.+)$|\1 "\2"|g' | \
mlr --csv -N --ifs " " put 'if($1=~"xml") {$4=$1;$5=$2}' \
then unsparsify \
then fill-down -f 4,5  \
then count-similar -g 4 \
then filter '($1=~"xml" && $count==1) || ($1!=~"xml" && $count>1)' \
then reorder -f 4,5,1,2,3 \
then put 'if($2=~"Fru"){$1="";$2="";$3=""}' \
then cut -x -f count

у вас будет

+------------------------+--------------------------------------+--------------------+------------------+----------------------------------------+
| 0bef-82-46-8a-9a0b.xml | Fruits/Mango Apple /Plum cherry date | 0bef-82-46-8a-9a0b | 5da-0-ba-c1-1a9  | Fruits/Pear Banana/Plum orange mango   |
| 0bef-82-46-8a-9a0b.xml | Fruits/Mango Apple /Plum cherry date | 0bef-82-46-8a-9a0b | ac-94-4ab-91-23  | Fruits/Pear Banana/Plum orange mango   |
| 0bef-82-46-8a-9a0b.xml | Fruits/Mango Apple /Plum cherry date | 0bef-82-46-8a-9a0b | 5z-94-ab-92-2f3  | Fruits/Pear Banana/Plum orange mango   |
| 952f-82-46-8a-9a0b.xml | Fruits/Mango                         | -                  | -                | -                                      |
| 3cff-82-46-8a-9a0b.xml | Fruits/Big Mango/Not Sweet           | -                  | -                | -                                      |
| 6m0k-82-46-8a-9a0b.xml | Fruits/Big Pear/Very Sweet           | -                  | -                | -                                      |
| 17a-42-df-c24.xml      | Fruits Market/Big Apple/Sweet        | 17a-42-df-c24      | 54-ba-4411-9-3d8 | Veg/Radish /Radish Carrot Celery Onion |
| 17a-42-df-c24.xml      | Fruits Market/Big Apple/Sweet        | 17a-42-df-c24      | 2da5-0-4a-b1-e89 | Veg/Radish /Radish Carrot Celery Onion |
| 17a-42-df-c24.xml      | Fruits Market/Big Apple/Sweet        | 17a-42-df-c24      | b7-94-4db-92-2f3 | Veg/Radish /Radish Carrot Celery Onion |
| 17a-42-df-c24.xml      | Fruits Market/Big Apple/Sweet        | 17a-42-df-c24      | 4d-67c-446-b5-ac | Veg/Radish /Radish Carrot Celery Onion |
| 17a-42-df-c24.xml      | Fruits Market/Big Apple/Sweet        | 17a-42-df-c24      | 2-8b-4det-87-769 | Veg/Radish /Radish Carrot Celery Onion |
+------------------------+--------------------------------------+--------------------+------------------+----------------------------------------+

Примечание:В качестве входных данных я использовал CSV без пустых строк

0
28.01.2020, 02:38

Теги

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