merging 2 csv files and sorting merged file

merging 2 csv files and sorting merged file

Post by Askar K » Mon, 02 May 2011 15:05:50


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... :( This a problem 1.
2nd is sorting - please advise me directions which I need read more...

Thanks in advance!

--
Posted via http://www.yqcomputer.com/
 
 
 

merging 2 csv files and sorting merged file

Post by Askar K » Mon, 02 May 2011 16:57:47

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

--
Posted via http://www.yqcomputer.com/

 
 
 

merging 2 csv files and sorting merged file

Post by Kresimir B » Mon, 02 May 2011 17:50:07

Or you can

cat file1 file2 ! sort

Just put pipe sign instead of ! :)

Kresimir Bojcic

www.kresimirbojcic.com