Определите поля по содержимому и удалите разделитель внутри

У меня есть файлы, разделенные запятыми, которые, к сожалению, содержат запятые внутри некоторых строк. Это делает их сортировку нетривиальной. См. предыдущий вопрос .

Независимо от сортировки, я полагаю, было бы лучше удалить такие заключенные в запятые, потому что они только будут представлять потенциальную опасность для каждой программы в моем конвейере.

Я только изучаю awk / gawk. Я решил, что хорошей стратегией будет:

  1. Определить поля по содержимому, а не по разделителю,как здесь .
  2. Удалите разделитель внутри полей, как здесь , помня о модификации, которая должна ограничить gsub одним столбцом

Затем я сделал следующую попытку сценарий sorter.awk, который я намеревался удалить запятые только внутри столбца 6:

BEGIN {
FPAT = "([^,]+)|(\"[^\"]+\")"

}

{gsub(/[,]/,"",$6)}1

Но когда я использую команду

gawk -f sorter.awk bugtest.csv> output.csv

, чтобы применить его к следующий файл, bugtest.csv:

1000,101,1,2,"VEN","Venezuela, Bolivarian Republic of",1967,22,4,99,0,0,1967-12-07,"R/22/2328A",0,1,"PRIVILEGES AND IMMUNITIES","TO ADOPT OPERATIVE PARAG. 2 OF DRAFT RESOL. (A/6965) ON DIPLOMATIC PRIVILEGES AND IMMUNITIES, WHICH PARAGRAPH URGES U.N. MEMBER-STATES WHO HAVE NOT YET DONE SO TO ACCEDE TO THE U.N. CONVENTION ON PRIVILEGES AND IMMUNITIES.",0,0,0,0,0,0,0,22027
1000,713,1,1,"TWN","Taiwan, Province of China",1967,22,4,99,0,0,1967-12-07,"R/22/2328A",0,1,"PRIVILEGES AND IMMUNITIES","TO ADOPT OPERATIVE PARAG. 2 OF DRAFT RESOL. (A/6965) ON DIPLOMATIC PRIVILEGES AND IMMUNITIES, WHICH PARAGRAPH URGES U.N. MEMBER-STATES WHO HAVE NOT YET DONE SO TO ACCEDE TO THE U.N. CONVENTION ON PRIVILEGES AND IMMUNITIES.",0,0,0,0,0,0,0,22027
100,101,1,2,"VEN","Venezuela, Bolivarian Republic of",1948,3,9,6,37,0,1948-11-07,"R/3/566C",0,1,"DISARMAMENT, NUCLEAR","TO ADOPT PARAGRAPH 7 OF THE USSR DRAFT RESOL. (A/723), SAID PARAGRAPH RECOMMENDING THE PROHIBITION OF ATOMIC WEAPONS INTENDED FOR AGGRESSION.",0,1,1,0,0,0,0,3023
1001,101,1,1,"VEN","Venezuela, Bolivarian Republic of",1967,22,1,101,0,0,1967-12-07,"R/22/2328B",0,0,"PRIVILEGES AND IMMUNITIES","TO ADOPT DRAFT RESOL. (A/6965) URGING U.N. MEMBER-STATES WHO HAVE NOT YET DONE SO TO ACCEDE TO THE U.N. CONVENTION ON (DIPLOMATIC) PRIVILEGES AND IMMUNITIES AND DEPLORING ALL DEPARTURES FROM THE RULES OF INTERNATIONAL LAW ON THE SUBJECT.",0,0,0,0,0,0,0,22028
1001,713,1,1,"TWN","Taiwan, Province of China",1967,22,1,101,0,0,1967-12-07,"R/22/2328B",0,0,"PRIVILEGES AND IMMUNITIES","TO ADOPT DRAFT RESOL. (A/6965) URGING U.N. MEMBER-STATES WHO HAVE NOT YET DONE SO TO ACCEDE TO THE U.N. CONVENTION ON (DIPLOMATIC) PRIVILEGES AND IMMUNITIES AND DEPLORING ALL DEPARTURES FROM THE RULES OF INTERNATIONAL LAW ON THE SUBJECT.",0,0,0,0,0,0,0,22028
1002,101,1,3,"VEN","Venezuela, Bolivarian Republic of",1967,22,11,50,51,0,1967-12-07,"R/22/2338A",1,1,"INTERNATIONAL YEAR FOR HUMAN RIGHTS","TO ADOPT THE AMENDMENT (A/L. 542) TO DRAFT RESOL. (A/7008) ON \INTERNATIONAL YEAR FOR HUMAN RIGHTS\\, WHICH AMENDMENT DELETES OPERATIVE PARAG.10.\""""",0,0,0,1,0,0,0,22029

output.csv выглядит так, без запятых:

1000 101 1 2 "VEN" "Venezuela Bolivarian Republic of" 1967 22 4 99 0 0 1967-12-07 "R/22/2328A" 0 1 "PRIVILEGES AND IMMUNITIES" "TO ADOPT OPERATIVE PARAG. 2 OF DRAFT RESOL. (A/6965) ON DIPLOMATIC PRIVILEGES AND IMMUNITIES, WHICH PARAGRAPH URGES U.N. MEMBER-STATES WHO HAVE NOT YET DONE SO TO ACCEDE TO THE U.N. CONVENTION ON PRIVILEGES AND IMMUNITIES." 0 0 0 0 0 0 0 22027
1000 713 1 1 "TWN" "Taiwan Province of China" 1967 22 4 99 0 0 1967-12-07 "R/22/2328A" 0 1 "PRIVILEGES AND IMMUNITIES" "TO ADOPT OPERATIVE PARAG. 2 OF DRAFT RESOL. (A/6965) ON DIPLOMATIC PRIVILEGES AND IMMUNITIES, WHICH PARAGRAPH URGES U.N. MEMBER-STATES WHO HAVE NOT YET DONE SO TO ACCEDE TO THE U.N. CONVENTION ON PRIVILEGES AND IMMUNITIES." 0 0 0 0 0 0 0 22027
100 101 1 2 "VEN" "Venezuela Bolivarian Republic of" 1948 3 9 6 37 0 1948-11-07 "R/3/566C" 0 1 "DISARMAMENT, NUCLEAR" "TO ADOPT PARAGRAPH 7 OF THE USSR DRAFT RESOL. (A/723), SAID PARAGRAPH RECOMMENDING THE PROHIBITION OF ATOMIC WEAPONS INTENDED FOR AGGRESSION." 0 1 1 0 0 0 0 3023
1001 101 1 1 "VEN" "Venezuela Bolivarian Republic of" 1967 22 1 101 0 0 1967-12-07 "R/22/2328B" 0 0 "PRIVILEGES AND IMMUNITIES" "TO ADOPT DRAFT RESOL. (A/6965) URGING U.N. MEMBER-STATES WHO HAVE NOT YET DONE SO TO ACCEDE TO THE U.N. CONVENTION ON (DIPLOMATIC) PRIVILEGES AND IMMUNITIES AND DEPLORING ALL DEPARTURES FROM THE RULES OF INTERNATIONAL LAW ON THE SUBJECT." 0 0 0 0 0 0 0 22028
1001 713 1 1 "TWN" "Taiwan Province of China" 1967 22 1 101 0 0 1967-12-07 "R/22/2328B" 0 0 "PRIVILEGES AND IMMUNITIES" "TO ADOPT DRAFT RESOL. (A/6965) URGING U.N. MEMBER-STATES WHO HAVE NOT YET DONE SO TO ACCEDE TO THE U.N. CONVENTION ON (DIPLOMATIC) PRIVILEGES AND IMMUNITIES AND DEPLORING ALL DEPARTURES FROM THE RULES OF INTERNATIONAL LAW ON THE SUBJECT." 0 0 0 0 0 0 0 22028
1002 101 1 3 "VEN" "Venezuela Bolivarian Republic of" 1967 22 11 50 51 0 1967-12-07 "R/22/2338A" 1 1 "INTERNATIONAL YEAR FOR HUMAN RIGHTS" "TO ADOPT THE AMENDMENT (A/L. 542) TO DRAFT RESOL. (A/7008) ON \INTERNATIONAL YEAR FOR HUMAN RIGHTS\\, WHICH AMENDMENT DELETES OPERATIVE PARAG.10.\" """" 0 0 0 1 0 0 0 22029

Итак, как добиться удаления разделителя в кавычках только из столбца 6? Это будет запятая после > Венесуэла и Тайвань для ясности.

0
задан 13 November 2020 в 21:39

0 ответов

Другие вопросы по тегам:

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