From: James Gray Date: 2008-01-11T00:12:01+09:00 Subject: Re: Multiline (block) CSV file processing On Jan 10, 2008, at 7:47 AM, Phil Rhoades wrote: > I am looking for suggestions for Ruby utilities (and gems?) for a > flexible, easy method of processing multi-line blocks of CSV text eg > in > a CSV file, lines 1-5 are the first block and lines 6-10 are the > second > block etc. Then for each block I want to: > > - print the first field of line 1 > - second field of line 2 > - fifth field of line 3 > - tenth field of line 4 > - twelfth field of line 5 > > as fields of a new line. Well, if I fully understand the request, I would use code like the following with the fastercsv gem: #!/usr/bin/env ruby -wKU require "rubygems" require "faster_csv" input = FCSV.open(ARGV.shift) output = FCSV.open("filtered.csv", "w") catch(:out_of_lines) do loop do lines = Array.new(5) { input.shift or throw :out_of_lines } output << lines.zip([0, 1, 4, 9, 11]).map { |line, i| line[i] } end end __END__ Hope that helps. James Edward Gray II