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 7040699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:56:58+00:00 2026-05-28T01:56:58+00:00

I have a CSV file formatted like this: id @ word @ information @

  • 0

I have a CSV file formatted like this:

id @ word @ information @ other information

Sometimes, the first column has repeat occurrences:

001 @ cat @ makes a great pet @ mice
002 @ rat @ makes a great friend @ cheese
003 @ dog @ can guard the house @ chicken
004 @ cat @ can jump very high @ fish

You can see, the first and last lines have duplicate data in column 2. I want to delete these duplicates (if column 2 is exactly the same) and merge the information contained in column three as well as the information contained in column four. The result is like this:

001 @ cat @ ① makes a great pet ② can jump very high @ ① mice ② fish
002 @ rat @ makes a great friend @ cheese
003 @ dog @ can guard the house @ chicken
  • I am using these symbols to number the data: “①”, “②”, “③”, etc., but “(1)”, “(2)”, “(3)”, etc. will be okay too.

How can I merge the data in the cells in so that all of the data from the third column is assembled together into one cell and the data in the fourth column is assembled together into one cell?

  • 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-28T01:56:58+00:00Added an answer on May 28, 2026 at 1:56 am

    I worked in ruby (doing this in bash would be kinda painful).

    First I wrote a spec to describe the problem:

    require 'rubygems'
    require 'rspec'
    require './chew'
    
    describe 'indentation' do
      it "should calculate appropriate padding (minimum 3)" do
        indentation(1).should == 3
        indentation(99).should == 3
        indentation(999).should == 3
        indentation(1000).should == 4
        indentation(1500).should == 4
        indentation(10000).should == 5
      end
    end
    
    describe 'chew' do
      it "should merge duplicate entries in a csv file" do
    
        input = <<-TEXT
    001 @ cat @ makes a great pet @ mice
    002 @ rat @ makes a great friend @ cheese
    003 @ dog @ can guard the house @ chicken
    004 @ cat @ can jump very high @ fish
        TEXT
    
        output = <<-TEXT
    001 @ cat @ (1) makes a great pet (2) can jump very high @ (1) mice (2) fish
    002 @ rat @ makes a great friend @ cheese
    003 @ dog @ can guard the house @ chicken
        TEXT
    
        chew(input).should == output
    
      end
    end
    

    Here’s a solution:

    #! /bin/bash/env ruby
    
    def merged_values(values)
      return values[0] if values.size == 1
      merged = []
      values.each_with_index do |value, i|
        merged << "(#{i+1}) #{value}"
      end
      merged.join(" ")
    end
    
    def indentation(count)
      [3, Math.log10(count) + 1].max.to_i
    end
    
    def chew(input)
    
      records = Hash.new {|hash, key| hash[key] = [[],[]]}
      input.split(/\n/).each do |row|
        row_number, key, first_value, second_value = row.split(/\s*@\s*/)
        records[key][0] << first_value
        records[key][1] << second_value
        records
      end
    
      row_number_format = "%0.#{indentation(records.size)}d"
    
      result = ""
      records.each_with_index do |record, i|
        key, values = record
        result << [
          row_number_format % (i+1),
          key,
          merged_values(values[0]),
          merged_values(values[1])
        ].join(" @ ") << "\n"
      end
      result
    
    end
    
    if $0 == __FILE__
      abort "usage: ruby chew.rb input_file" unless ARGV.size == 1
      puts chew(File.read(ARGV[0]))
    end
    

    I opted for the simpler numbering scheme, because what happens if there are more than 50 values to merge? http://en.wikipedia.org/wiki/Enclosed_alphanumerics

    I took the liberty of increasing the left padding when there are lots of records.

    • 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, formatted like this: 0001 @ word @ some information
I have a CSV file formatted just like this: name,color,tasty,qty apple,red,true,3 orange,orange,false,4 pear,greenish-yellowish,true,1 As
I have a csv file that looks something like this (actual file has many
I have CSV file which has timestamp in the first column as shown below
I have a CSV file that is formatted like: 0.0023709,8.5752e-007,4.847e-008 and I would like
I have a CSV file that has only 1 column, but has close to
I have csv file with a line that looks something like this: ,,,,,,,,,, That's
I have a dictionary file formatted like this: A B [C] D Where a
I have csv file that looks like this: artist,year,id,video_name,new_video_id,file_root_name,video_type ,,,,,, Clay Aiken,1,clay_aiken,Sorry Seems To
I have a csv file that has like 30,000 rows in it. It also

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.