我有一个非常小的文件错误测试.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
我想按年份 (第 7 列) 进行排序。我尝试:
sort -o Votes2.csv -t, -nk7 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
我已经设法给你一个小文件,但除此之外,我还是一名新手,完全不知道是什么原因导致了这种情况!
编辑:Steeldriver 提供了很大帮助,他指出此文件中有带引号的逗号。如果带引号的逗号是原因,他应该在某处比较字符串!为什么这不与 -n 选项冲突?难道不应该让他找到第 7 行不包含数字的行并指示他删除前一个逗号吗?
答案1
主要问题是第 6 列和第 7 列之间有一个额外的逗号来组织数据:"Venezuela, Bolivarian Republic of"
。因此,您还必须考虑该列:
sort -o Votes2.csv -t, -nk8 bugtest.csv
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
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
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
这对我行得通。