reading a file having no carriage return

U

Une bévue

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
 
C

ChrisH

Une said:
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

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
 
U

Une bévue

ChrisH said:
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
 
U

Une bévue

Tim Hoolihan said:
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...
 
A

Alain FELER

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

Une bévue

Alain FELER said:
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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top