
Untitled
By: a guest on
Jun 17th, 2012 | syntax:
None | size: 0.50 KB | hits: 18 | expires: Never
How do I retrieve only lines with specific words or phrases?
lines = File.readlines("myfile.txt")
the red queen of hearts.
yet another sentence.
and this has no relevant words.
the blue sky
the white chocolate rabbit.
another irrelevant line.
the red queen of hearts.
the blue sky
the white chocolate rabbit.
lines = File.readlines("myfile.txt").grep(/red|rabbit|blue/)
/^.*(red|rabbit|blue).*$/
lines.each do |line|
if line.include? red or rabbit or blue
puts line
end
end