From: Brian Candler Date: 2007-02-07T18:41:11+09:00 Subject: Re: Parse csv similar file On Wed, Feb 07, 2007 at 05:47:26PM +0900, Rebhan, Gilbert wrote: > i get a folder > > /timestamp > metafile.txt > /INCLIB > /PLI > > metafile looks like that = > APLVZDT;INCLIB;E050441;AP013;240107;0730 > AP400ER;INCLIB;E023889;AP013;240107;0730 > AP540RBP;INCLIB;E052337;AP013;240107;0730 > AP700PA;INCLIB;E050441;AP013;240107;0730 > ... more lines > > field 1 is a filename > field 2 is a foldername, shows whether path is /INCLIB/file or /PLI/file > field 3 is a ticketnr > field 4 is a username > field 5 is a date > field 6 is a timestamp > > > i need to parse the metafile and = > > 1. create a folderstructure for every ticketnr that occurs, f.e. > > /E050441 > /INCLIB > /PLI > > and put all the files that belong to that ticket > (means the line with the filename contains that ticketnr) > in the subfolder which is field 2 > > 2. create a file in the root of the /ticketnr folder > which contains the rest of a dataset (line), means = > > field 4 > field 5 > field 6 > > which are the same for every file with the same ticketnr > > the format might look like > > user=... > date=... > time=... > > have to decide it later. > > > I thought with = > > File.open("mycsvfile", "r").each do |line| > if line =~ /(\w+.?);(\w+);(\w+);(\w+);(\w+);(\w+)/ > > efas<<$3.to_s<<',' unless efas.include?($3.to_s) > > i get an array with all ticketnr > then i create a folderstructure for every index in that array > and put the files in it, but i don't get it. > > Any ideas ? I'd do all the work on-the-fly. Untested code: require 'fileutils' SRCDIR="/path_to_src" DSTDIR="/path_to_dst" def copy_ticket(filename, folder, ticket, user, date, time) srcdir = SRCDIR + File::SEPARATOR + folder dstdir = DSTDIR + File::SEPARATOR + ticket + File::SEPARATOR + folder FileUtils.mkdir_p(dstdir) FileUtils.cp(srcdir + File::SEPARATOR + filename, dstdir + File::SEPARATOR + filename) # write out status file statusfile = dstdir + File::SEPARATOR + "status.txt" unless FileTest.exists?(statusfile) File.open(statusfile, "w") do |sf| sf.puts "user=#{user}" sf.puts "date=#{date}" sf.puts "time=#{time}" end end end def process_meta(f) f.each_line do |line| next unless line =~ /^(\w+);(\w+);(\w+);(\w+);(\w+);(\w+)$/ copy_ticket($1,$2,$3,$4,$5,$6) end end # Main program File.open("mycsvfile") do |f| process_meta(f) end If you want to build up a hash of ticket IDs seen, you can do that in process_meta as well. I'd pass in an empty hash, and update it in the each_line loop. HTH, Brian.