Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ngm
Created November 27, 2011 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngm/1397667 to your computer and use it in GitHub Desktop.
Save ngm/1397667 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks - Ruby - Day 2 - Grep
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