From: Brian Adkins Date: 2008-01-11T04:19:57+09:00 Subject: Re: Multiline (block) CSV file processing On Jan 10, 1:40 pm, Brian Adkins wrote: > On Jan 10, 11:18 am, Phil Rhoades wrote: > > > 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 This is a little more general. def block_extractor file, fields, splitter = lambda {|line| line.split } while !file.eof result = [] fields.each do |field| line = file.readline result << splitter.call(line.chomp)[field-1] if field end yield result end end File.open("data.txt", "r") do |file| block_extractor(file, [1,2,5,10,12]) do |fields| puts fields.join(' ') end end