[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

reading a file having no carriage return

pere.noel

6/16/2006 4:18:00 PM


how to read a file having no carriage return ?

in that file i need only to found a string like :

news.free.fr

thats to say news.xxx.yyy

if i do :
File.open("#{file}").each { |l|
p l #if l =~ /news\./
h=l.split(" ")
p h.length
}

i get nothing

--
une bévue
6 Answers

Chris Hulan

6/16/2006 7:45:00 PM

0

Une bévue wrote:
> how to read a file having no carriage return ?
>
> in that file i need only to found a string like :
>
> news.free.fr
>
> thats to say news.xxx.yyy
>
> if i do :
> File.open("#{file}").each { |l|
> p l #if l =~ /news\./
> h=l.split(" ")
> p h.length
> }
>
> i get nothing
>
> --
> une bévue

I'd try:
matches = []
File.open(file,'r'){|f|
matches = f.read.scan(/news\./) #read all file contents and scan for
matches
} #file is automatically closed on block exit
#do something with matches...

But you need a correct RegExp, /news\./ will match 'news.' and the
'xxx.yyy' will not be included in the results

pere.noel

6/16/2006 8:02:00 PM

0

ChrisH <chris.hulan@gmail.com> wrote:

> I'd try:
> matches = []
> File.open(file,'r'){|f|
> matches = f.read.scan(/news\./) #read all file contents and scan for
> matches
> } #file is automatically closed on block exit
> #do something with matches...

with :
matches = []
File.open(file,'r'){|f|
matches = f.read.scan(/news\./) #read all file contents and scan
for matches
p matches.length
p matches

i got :
0
[]

> But you need a correct RegExp, /news\./ will match 'news.' and the
> 'xxx.yyy' will not be included in the results

no prob for me and the regexp

--
une bévue

Tim Hoolihan

6/16/2006 8:51:00 PM

0

put in a file news.txt:
news.test.com
news.test1.com

run this script:

#dump out the file
newstext = File.open("news.txt").read
puts "newstest:",newstext,"\n"
matches=newstext.scan(/(news\.[\S]+)\s/)
#dump out matches
puts "matches:"
matches.each{|m| puts m}


Une bévue wrote:
> ChrisH <chris.hulan@gmail.com> wrote:
>
>> I'd try:
>> matches = []
>> File.open(file,'r'){|f|
>> matches = f.read.scan(/news\./) #read all file contents and scan for
>> matches
>> } #file is automatically closed on block exit
>> #do something with matches...
>
> with :
> matches = []
> File.open(file,'r'){|f|
> matches = f.read.scan(/news\./) #read all file contents and scan
> for matches
> p matches.length
> p matches
>
> i got :
> 0
> []
>
>> But you need a correct RegExp, /news\./ will match 'news.' and the
>> 'xxx.yyy' will not be included in the results
>
> no prob for me and the regexp
>

pere.noel

6/17/2006 6:01:00 AM

0

Tim Hoolihan <tim@hoolihan.net> wrote:

> put in a file news.txt:
> news.test.com
> news.test1.com

someone gave me a solution :
egrep -i -o --binary-files=text -e 'news\.\w+\.\w+' /Users/yvon/MacSOUP_eclipse/eclipse/..namedfork/rsrc
___the_magic_______________________^^^^^^^^^^^^^^^^^^

the file being :
/Users/yvon/MacSOUP_eclipse/eclipse

that's something specific to MacOS X...
--
une bévue

alain.feler

6/19/2006 8:42:00 PM

0

open('test.txt').read.scan(/azer\.tyui\.op/) {|c| print c,"\n"}
# I have tried it with a 19 943 385 bytes test.txt...

pere.noel

6/20/2006 5:52:00 AM

0

Alain FELER <alain.feler@wanadoo.fr> wrote:

> open('test.txt').read.scan(/azer\.tyui\.op/) {|c| print c,"\n"}
> # I have tried it with a 19 943 385 bytes test.txt...

fine ;-)

it works also for me :)
--
une bévue