3

I have the following input:

no,zadrar,MENTOR,[email protected],AGRATE
no,mittalsu,MENTOR,[email protected],GREATER NOIDA
no,abousamr,CADENCE,[email protected],CROLLES
no,lokinsks,MENTOR,[email protected],CROLLES
no,billys,MENTOR,[email protected],CROLLES
no,basiles1,CADENCE,[email protected],CASTELLETTO
no,cesaris1,CADENCE,[email protected],CROLLES

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

no,abousamr,CADENCE,[email protected],CROLLES
no,lokinsks,MENTOR,[email protected],CROLLES
no,billys,MENTOR,[email protected],CROLLES

I tried with:

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

But I get:

    [email protected]
    [email protected]
    [email protected]
0

2 Answers 2

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).

4
  • 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, 2018 at 9:40
  • You are right, better to remove the cut output to avoid confusion. Aug 17, 2018 at 9:44
  • your answer is still wrong, not sure if you got what I wanted to convey
    – Sundeep
    Aug 17, 2018 at 9:47
  • your answer give one of multiple value from column 4
    – kawther
    Aug 17, 2018 at 9: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
3
  • 1
    Very nice, could you explain more why the input 2 times?
    – nbari
    Aug 17, 2018 at 9: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. Aug 17, 2018 at 9:20
  • 1
    @RavinderSingh13 This seems to be the best solution I go for, thank you
    – kawther
    Aug 17, 2018 at 9:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.