From: Gregory Seidman Date: 2007-07-01T22:21:12+09:00 Subject: Re: awk regexp search On Sun, Jul 01, 2007 at 10:13:16PM +0900, baptiste Augui? wrote: > Hi, > > The last bit of a bash program is still resisting me. Here is the > code I used before: > > awk '/scattering efficiency/{print $4}' ../OUTPUTFILES/Output.dat > > How would you do that in Ruby? I just need to locate this regexp in > the file, and get the following value in the same line. I've tried > something like, > > output_file=IO.readlines('../OUTPUTFILES/Output.dat').to_s > myarray = output_file.each_line(/scattering efficiency/){|elt| print > elt.to_s, '\t'} > > but it clearly doesn't work. First off, don't be too quick to abandon awk. I took a dozen lines of Ruby someone wrote that did almost what I wanted and ported/replaced it with three lines of sh/awk. For what awk can do, it is excellent. To set up the equivalent of the awk code above, you want something like this (note: untested): ARGF.each { |line| case line when /scattering efficiency/ puts line.split(/\s+/)[3] #note 3 instead of 4 end } Note that this basic framework does not support awk's /regex1/,/regex2/ notation that captures lines between (and including) the lines matching those regular expressions. > Best regards, > baptiste --Greg