From: Simon Strandgaard Date: 2004-03-11T06:29:42+09:00 Subject: Re: Novice question, tranlate awk code to ruby code On Wed, 10 Mar 2004 16:04:21 -0300, Horacio Castellini wrote: > I'm a novice ruby user, but ancient user in Linux world. We assume > have a data file with two data field in two columns. In awk program when I > like time them a script code may be: > > { > print $1*$2 > } > > for example I can use "% cat foo.dat|awk -f times.awk" d'accorde. Well, > how can I do this in ruby code? > Of cause this could be simplified a lot.. but here is a simplistic solution. Is this what you want ? BTW: welcome to ruby ;-) -- Simon Strandgaard server> ruby a.rb 1 2 3 2 4 6 3 6 9 4 8 12 server> expand -t2 a.rb # create a dummy data file File.open("data", "w+") do |file| file.write <<-DATA.gsub(/^\s+/, "") 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 4 1 4 2 4 3 DATA end # process data lines = IO.readlines("data") lines.each do |line| m = /(\d+)\s+(\d+)/.match(line) unless m puts "cannot compute" next end lhs = m[1].to_i rhs = m[2].to_i p lhs * rhs end server>