From: Brian Adkins Date: 2008-01-11T03:45:11+09:00 Subject: Re: Multiline (block) CSV file processing 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 File.open("data.txt", "r") do |file| i = 0 file.each_line do |line| print line.chomp.split[[1, 2, 5, 10, 12][i]-1] + ' ' puts '' if (i = (i + 1) % 5) == 0 end end Or, the following might be more fun: def each_block file while !file.eof result = [] 5.times { result << file.readline.chomp } yield result end rescue end File.open("data.txt", "r") do |file| each_block(file) do |block| [1, 2, 5, 10, 12].zip(block).each do |field, line| print line.split[field-1] + ' ' end puts '' end end Brian Adkins