Created
November 27, 2011 15:01
-
-
Save ngm/1397667 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks - Ruby - Day 2 - Grep
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts | |
print "File name: " | |
filename = gets.chomp | |
puts | |
print "Search term: " | |
phrase = gets.chomp | |
File.open(filename, "r") do |file| | |
file.readlines.each_with_index do |line,index| | |
if /#{phrase}/.match(line) | |
puts | |
puts "Line #{index+1} matches." | |
puts line.gsub(/(#{phrase})/, '**\1**') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment