Output first unique record in csv file

Hi,

I have to output a new csv file from an input csv file with first unique value in the first column.

input csv file

color   product id status
green   102        pass
yellow  201        hold
yellow  202        keep
green   101        ok
green   103        hold
yellow  203        wait

output csv file

color	product id	status
green	102	pass
yellow	201	hold

Please suggest how I can do this.

if you have Ruby on your system

array=[]
File.open("file").each_line do |line|
  first = line.split(/\s+/).first
  if not array.include?(first) 
    puts line
    array << first 
  end       
end
# ruby test.rb 
color product id status
green 102 pass
yellow 201 hold

Hi Kurumi,

I don't have Ruby on my system. Is there any other solution please?

Regards,
Chris

be patient then.

Using awk:

awk 'NR==1||!A[$1]++' file

or simply:

awk '!A[$1]++' file
1 Like

Yoda, many thanks for your prompt and useful reply