3

I have the following input:

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

I want to get only the lines where column 4 is unique:

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

I tried with:

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

But I get:

    selim@xt.com
    sergey@xt.com
    billy@xt.com
kawther
  • 184
  • 2
  • 3
  • 16

2 Answers2

6

You can use simply the options provided by the sort command:

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

As you can see in the man page, option -u stands for "unique", -t for the field delimiter, and -k allows you to select the location (key).

elcortegano
  • 2,444
  • 11
  • 40
  • 58
  • no, OP wants only unique line.. in other words, any 4th column value which is present more than once should not be in output.. for ex: `rossana` is there twice, should not be in output but your solution will give one of them in output – Sundeep Aug 17 '18 at 09:40
  • You are right, better to remove the `cut` output to avoid confusion. – elcortegano Aug 17 '18 at 09:44
  • your answer is still wrong, not sure if you got what I wanted to convey – Sundeep Aug 17 '18 at 09:47
  • your answer give one of multiple value from column 4 – kawther Aug 17 '18 at 09:57
3

Could you please try following(reading Input_file 2 times).

awk -F',' 'FNR==NR{a[$4]++;next} a[$4]==1'  Input_file Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    Very nice, could you explain more why the input 2 times? – nbari Aug 17 '18 at 09:18
  • @nbari, 2 times I read first time it will take the count of lines and store them in array and second time to execute the condition if a count is `1` it should print line then only. – RavinderSingh13 Aug 17 '18 at 09:20
  • 1
    @RavinderSingh13 This seems to be the best solution I go for, thank you – kawther Aug 17 '18 at 09:54