Hi Everyone,
My name is Jay, I’m new to the forum as well as fairly new to Ruby. I am
writing a program with File.open to read a text file, however need help
on a code that would only read certain rows of this large file. I am
basically only trying to get the rows where the first column has ‘cdtr’
(example)
in the data field.
I appreciate any help on this and thank you in advance.
Jay
Hi Everyone,
My name is Jay, I’m new to the forum as well as fairly new to Ruby. I am
writing a program with File.open to read a text file, however need help
on a code that would only read certain rows of this large file. I am
basically only trying to get the rows where the first column has ‘cdtr’
(example)
in the data field.
I appreciate any help on this and thank you in advance.
You could use File#foreach and a condition on the line, maybe a
regular expression. For example:
File.foreach(“file.txt”) do |line|
next unless line =~ /^cdtr/
do_something_useful_with line
end
You will most probably need to tweak the regular expression to your
needs. In the example I’m checking that the line starts with “cdtr”,
but maybe your condition is more complex, depending on the format of
your file.