Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6371325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:03:00+00:00 2026-05-25T01:03:00+00:00

I have a CSV file with several thousand customer’s details. I would like to

  • 0

I have a CSV file with several thousand customer’s details. I would like to extract duplicated customers based on having the same values for selected headers.

For example, I would like to extract all customers where there exists more than one record with the same ‘surname’ and ‘postcode’.

"surname","postcode","other-stuff-that-doesn't-matter"...
"smith",  "AB1 2CD", "dxfh"...
"smith",  "AB1 2CD", "98sf"...
"jones",  "BC2 3DE", "as0j"...
"jones",  "BC2 3DE", "9as6"...
"blogs",  "BC2 3DE", "9as6"...

Based on the above, the program would return a new CSV like so:

"surname","postcode","other-stuff-that-doesn't-matter"...
"smith",  "AB1 2CD", "dxfh"...
"smith",  "AB1 2CD", "98sf"...
"jones",  "BC2 3DE", "as0j"...
"jones",  "BC2 3DE", "9as6"...

EDIT

Thanks for the help so far. I think I have a working solution, but I’m interested to know if this can be optimised (I’m sure it can!).

set_one    = Set.new
set_two    = Set.new
duplicates = Array.new
headers    = nil

CSV.foreach('customers.csv', :headers => true, :header_converters => :symbol) do |row|
  headers = row.headers unless headers
  values = [row[:surname], row[:post_code]]
  if set_one.include? values
    set_two << values
  else
    set_one << values 
  end
end

CSV.foreach('customers.csv', :headers => true, :header_converters => :symbol) do |row|
  values = [row[:surname], row[:post_code]]
  if set_two.include? values
    duplicates << row
  end
end

CSV.open("duplicate-customers.csv", "wb") do |csv|
  csv << headers
  duplicates.each { |dupe| csv << dupe }
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:03:01+00:00Added an answer on May 25, 2026 at 1:03 am

    Let’s read in the csv first (doesn’t handle escapes or quotes, just an example)

    csv = []
    columns = []
    File.read('csv.file') do |row|
      if csv.empty?
        columns=row.split(',')
      else
        row_data={}
        row.split(',').each_with_index do |c,i|
          row_data[columns[i]] = c
        end
        csv << row_data
      end
    end
    

    Ok, what do we do with the data? It looks like:

    [{'surname' => 'smith', 'postcode' => '1234', 'otherstuff' => 'xyz' },
     {'surname' => 'jones', 'postcode' => '1234', 'otherstuff' => 'xyz' },
     {'surname' => 'smith', 'postcode' => '2345', 'otherstuff' => 'xyz' },
     {'surname' => 'smith', 'postcode' => '1234', 'otherstuff' => 'xyz' }]
    

    How about something like:

    csv.select do |c| 
      csv.any? do |s| 
        s['surname'].eql?(c['surname']) && s['postcode'].eql?(c['postcode']) 
      end
    end
    

    Ok that’s slow and not clever. Let’s proceed to solution 2, generate a hash key from the data we want to check for uniqueness:

    sneakyhash={}
    csv.each do |row|
      magic_string = [row['surname'], row['postcode']].join("--MaGiCaL--SpLiTTinG--StRiNG--")
      if sneakyhash[magic_string].nil?
        sneakyhash[magic_string] = 1
      else
        puts "this guy looks suspicious: " + row.join(,)
      end
    end
    

    Far from optimal, but just thinking out loud here. If it’s a “one time only” kind of thing and you just need to parse a file, go with what you can come up with.

    What you probably want to do is store this identifying string in an array or hash while reading the csv in and see if current row matches any of the stored unique rows and do something if it does.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a CSV file that contains several lines like this one : bar,foo,
I have a CSV file with several entries, and each entry has 2 unix
I have a CSV file that goes something like this: ['Name1', '', '', '',
I have a csv file, and I want to extract the each column a
I have a dump of several Postgresql Tables in a selfcontained CSV file which
I have a CSV file which contains an ID and several other columns. I
I have several datasets that I would like to delete after my SAS procedure
I currently have a .csv file with several unlabeled columns of data, which to
I'm trying to import a csv file in R. I have done it several
I have a 2 GB CSV file that has a few columns and several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.