Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Advertisement
Add Comment
Please, Sign In to add comment