From: James Gray Date: 2008-01-11T02:34:11+09:00 Subject: Re: Multiline (block) CSV file processing On Jan 10, 2008, at 10:18 AM, Phil Rhoades wrote: > James, > > > On Fri, 2008-01-11 at 00:12 +0900, James Gray wrote: >> 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. > > > Thanks but not quite - say my input file is: > > 1 2 3 4 5 6 7 8 9 a b c > 11 12 13 14 15 16 17 18 19 d e f > 21 22 23 24 25 26 27 28 29 g h i > 31 32 33 34 35 36 37 38 39 j k l > 41 42 43 44 45 46 47 48 49 m n o > > The output should be: > > 1 12 25 j o Surely, I got you close enough to finish it off, right? ;) If you're saying that your file is whitespace separated, as you show above set :col_sep for FasterCSV. If your data really doesn't contain quoted fields as shown above, you probably don't a CSV parser at all. You can read with split() and write with join(). If you run into problems or have more specific questions, ask and I'll do my best to help. James Edward Gray II