[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: awk regexp search

dblack

7/1/2007 1:25:00 PM

5 Answers

Robert Dober

7/1/2007 2:51:00 PM

0

On 7/1/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Sun, 1 Jul 2007, 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,
> >

> $ ruby -ne 'puts $1 if /scattering efficiency\s+(\S+)/'
> scattering efficiency blah
> => blah
> nothing
> this has scattering efficiency just like the other one
> => just
>
The problem is that we have no idea where "scattering efficency" is
relatively to $4 :(
However

ruby -ane 'puts $F[3] if /scattering efficency/' ../ton/beau/fichier

does the same as the awk script above

Side Remark:
domage que l'on ne puisse utiliser mes options préfèrées: -anpe ;)


Robert

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

baptiste Auguié

7/1/2007 3:27:00 PM

0

Thanks everybody,


this piece of code works fine for me,


output_file.each { |line|
case line
when /scattering efficiency/
qsca << line.split(/\s+/)[4]
end
}

although I realize now that "output_file" may contain a duplicate of
the line I want to extract. How can I specify to take only the last
occurrence?

The file to parse looks something like that,

> a few lines ...
>
>
> scattering efficiency = 2.8009E-08
> extinction efficiency = 4.9374E-06
>
> some lines in between ...
>
>
> scattering efficiency = 2.7957E-08
> extinction efficiency = 4.9374E-06
> ...



On 1 Jul 2007, at 15:51, Robert Dober wrote:

> On 7/1/07, dblack@wobblini.net <dblack@wobblini.net> wrote:
>> Hi --
>>
>> On Sun, 1 Jul 2007, 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,
>> >
>
>> $ ruby -ne 'puts $1 if /scattering efficiency\s+(\S+)/'
>> scattering efficiency blah
>> => blah
>> nothing
>> this has scattering efficiency just like the other one
>> => just
>>
> The problem is that we have no idea where "scattering efficency" is
> relatively to $4 :(
> However
>
> ruby -ane 'puts $F[3] if /scattering efficency/' ../ton/beau/fichier
>
> does the same as the awk script above
>

this piece of code needs to be part of a script, not a one line call -
> how would that work in a Ruby script?

> Side Remark:
> domage que l'on ne puisse utiliser mes options préfèrées: -anpe ;)

; )


>
>
> Robert

Thanks,

baptiste


Robert Dober

7/1/2007 6:09:00 PM

0

On 7/1/07, baptiste Auguié <ba208@exeter.ac.uk> wrote:
> > ruby -ane 'puts $F[3] if /scattering efficency/' ../ton/beau/fichier
> >
> > does the same as the awk script above
> >
>
> this piece of code needs to be part of a script, not a one line call -
> > how would that work in a Ruby script?
I am not sure I understand, this command can be put into a bash script
as the awk example above, if however you want to replace the bash
script by a Ruby script it will be necessary to read the File
explicitly

puts File.readlines("/le/beau/fichier"/).grep(/scattering efficency/).
map{|line| line.split[3]}

or only the first

puts File.readlines("/le/beau/fichier"/).grep(....).first.split[3]

or if performance is an issue and you do not want to read further lines

File.each("/le/grand/fichier") do
|line|
next unless /.../ === line
puts line.split[3]
break
end

Cheers
Robert
>
> > Side Remark:
> > domage que l'on ne puisse utiliser mes options préfèrées: -anpe ;)
>
> ; )
>
>
> >
> >
> > Robert
>
> Thanks,
>
> baptiste
>
>
>


--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

baptiste Auguié

7/1/2007 6:30:00 PM

0


On 1 Jul 2007, at 19:09, Robert Dober wrote:

>
> or only the first
>
> puts File.readlines("/le/beau/fichier"/).grep(....).first.split[3]


Perfect! exactly what I wanted (with last instead of first). I just
didn't know about this grep command in Ruby, basically.

Thanks,

baptiste


dblack

7/1/2007 7:54:00 PM

0