从csv文件中的列获取唯一值

时间:2018-08-17 09:11:26

标签: shell csv awk uniq

我有以下输入内容:

CREATE DEFINER=`your role` PROCEDURE / FUNCTION ()
    SQL SECURITY DEFINER -- that's the default
begin
    ....
end

我只想获取第4列唯一的行:

no,zadrar,MENTOR,rossana@xt.com,AGRATE
no,mittalsu,MENTOR,rossana@xt.com,GREATER NOIDA
no,abousamr,CADENCE,selim@xt.com,CROLLES
no,lokinsks,MENTOR,sergey@xt.com,CROLLES
no,billys,MENTOR,billy@xt.com,CROLLES
no,basiles1,CADENCE,stephane@xt.com,CASTELLETTO
no,cesaris1,CADENCE,stephane@xt.com,CROLLES

我尝试过:

no,abousamr,CADENCE,selim@xt.com,CROLLES
no,lokinsks,MENTOR,sergey@xt.com,CROLLES
no,billys,MENTOR,billy@xt.com,CROLLES

但是我得到了

awk -F"," '{print $4}' $vendor.csv | sort | uniq -u

2 个答案:

答案 0 :(得分:2)

请尝试以下操作(读取Input_file 2次)。

awk -F',' 'FNR==NR{a[$4]++;next} a[$4]==1'  Input_file Input_file

答案 1 :(得分:1)

您可以简单地使用sort命令提供的选项:

sort -u -t, -k4,4 file.csv

man页中可以看到,选项-u代表“唯一”,-t代表字段定界符,而-k则允许您选择位置(密钥)。