From: Phil Rhoades Date: 2008-01-11T01:18:59+09:00 Subject: Re: Multiline (block) CSV file processing 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 Regards, Phil. -- Philip Rhoades Pricom Pty Limited (ACN 003 252 275 ABN 91 003 252 275) GPO Box 3411 Sydney NSW 2001 Australia Fax: +61:(0)2-8221-9599 E-mail: phil@pricom.com.au