help with regular expressions

V

Vandana

Hi all,

I'm fairly new to programming with regular expressions. I would like
some help with the
following. Consider the format given below as what I will receive in a
file.

While reading from the file, Im interested only in reading those lines
that have the forward-slash' in
them (lines 9,10,12, 13). How can I do this in ruby?

1. -------------------------------
2. Version 2.05 bla bla
3. bla bla
4. bla
5.
6. ------------------------------
7. xx 0.0 4.5 6.7
8. xx (yy) 2.0 4.5 5.6
9. a/b/c/d () 6.0 2.0 3.4
10. a/b/c/d (yy)
11.
12. h/j/k/l/m 5.0 9.0 8.9
13. h/j/k/l/m ()
14.
15. xx 0.0 0.0 0.0
16. yy 8.9 8.9 8.0
17. ---------------------------------------


Thank you for your time & help
Vandana
 
S

Siep Korteling

Vandana said:
Hi all,

I'm fairly new to programming with regular expressions. I would like
some help with the
following. Consider the format given below as what I will receive in a
file.

While reading from the file, Im interested only in reading those lines
that have the forward-slash' in
them (lines 9,10,12, 13). How can I do this in ruby?

(...)

You don't need a regular expression for this. To select the lines which
include a slash, you can do

file = File.new( 'D:/1.txt' )
slashlines = file.select {|line| line.include?( '/' )}
file.close
puts slashlines


The select method will read each line and put each line for which the
block is true in an array.

hth,

Siep
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

You don't need a regular expression for this. To select the lines which
include a slash, you can do

file = File.new( 'D:/1.txt' )
slashlines = file.select {|line| line.include?( '/' )}
file.close
puts slashlines


The select method will read each line and put each line for which the
block is true in an array.


Siep, I can't find any documentation on File#select, and IO#select is not
documented as doing what you show. Could you provide more info about it?

Thanks,
Craig
 
M

Michael Guterl

Siep, I can't find any documentation on File#select, and IO#select is not
documented as doing what you show. Could you provide more info about it?
It is a method from Enumerable.

ri Enumerable#select

Michael Guterl
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

It is a method from Enumerable.

ri Enumerable#select


D'oh, didn't think of Enumerable for some reason. Time to learn more about
file I/O.

Thanks,
Craig
 
T

Todd Benson

Siep, I can't find any documentation on File#select, and IO#select is not
documented as doing what you show. Could you provide more info about it?

I couldn't find it explicitly in the docks either, but if you do
File.ancestors, you will see that Enumerable is in there.

To the OP, you could also do (for regex)...

slashlines = my_file.select {|line| line =~ /\//}


Todd
 
W

William James

Vandana said:
Hi all,

I'm fairly new to programming with regular expressions. I would like
some help with the
following. Consider the format given below as what I will receive in a
file.

While reading from the file, Im interested only in reading those lines
that have the forward-slash' in
them (lines 9,10,12, 13). How can I do this in ruby?

1. -------------------------------
2. Version 2.05 bla bla
3. bla bla
4. bla
5.
6. ------------------------------
7. xx 0.0 4.5 6.7
8. xx (yy) 2.0 4.5 5.6
9. a/b/c/d () 6.0 2.0 3.4
10. a/b/c/d (yy)
11.
12. h/j/k/l/m 5.0 9.0 8.9
13. h/j/k/l/m ()
14.
15. xx 0.0 0.0 0.0
16. yy 8.9 8.9 8.0
17. ---------------------------------------


Thank you for your time & help
Vandana

Awk:

awk "/\//" myfile


Ruby:

ruby -ne "print if /\//" myfile


--
 
S

Sebastian Hungerecker

Vandana said:
While reading from the file, Im interested only in reading those lines
that have the forward-slash' in
them (lines 9,10,12, 13). How can I do this in ruby?

file.grep %r(/)
or
file.grep /\//

HTH,
Sebastian
 
P

Peña, Botp

From: Todd Benson [mailto:[email protected]]=20
# I couldn't find it explicitly in the docks either, but if you do
# File.ancestors, you will see that Enumerable is in there.


qri can find it

botp@botp-desktop:~$ qri File#select
------------------------------------------------------ Enumerable#select
enum.find_all {| obj | block } =3D> array
enum.select {| obj | block } =3D> array
 
V

Vandana

Thanks to all!

I used f.grep(/\//) to read the lines that were needed.

Thanks once more.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top