Merging 2 csv files and sorting merged file

Hello all!

Things I want to do:

  1. merging 2 csv files
  2. sorting new merged file
  3. delete some columns from a new merged file

1st csv file1:

11101,3310000,IKANIKEISAIGANAIBAAI,NISHI-KU
SAITAMA-SHI,SAITAMA,0,0,0,0,0,0
11101,3310058,IIDA,NISHI-KU SAITAMA-SHI,SAITAMA,0,0,0,0,0,0
11101,3310068,IIDASHINDEN,NISHI-KU SAITAMA-SHI,SAITAMA,0,0,0,0,0,0

2nd csv file2:

12101,2600000,IKANIKEISAIGANAIBAAI,CHUO-KU CHIBA-SHI,CHIBA,0,0,0,0,0,0
12101,2600852,AOBACHO,CHUO-KU CHIBA-SHI,CHIBA,0,0,0,0,0,0
12101,2600804,AKAICHO,CHUO-KU CHIBA-SHI,CHIBA,0,0,0,0,0,0

The code below:

require “fileutils”
FileUtils.cp(“file1.csv”, “file3.csv”)

File.open(“file3.csv”, “a+”)
File.open(“file2.csv”, “r”) do |file|
file.each_line do |line|
puts line
end
end

1st I made a copy of the 1st file, and then I try just to add the
content of the 2nd file. At the moment I can only print the content of
the 2nd file, but can’t to add this the file3… :frowning: This a problem 1.
2nd is sorting - please advise me directions which I need read more…

Thanks in advance!

Here I post the answerI got from japanese mailing list,
which worked for me

out = File.open(“file3.csv”, “a+”)
File.open(“file2.csv”, “r”) do |file|
file.each_line do |line|
out.puts line # 出力
end
end

OR

File.open(“file3.csv”, “a+”) do |out|
out.print IO.read(‘file2.csv’)
end

Or you can

cat file1 file2 ! sort

Just put pipe sign instead of ! :slight_smile:

Kresimir Bojcic

www.kresimirbojcic.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs