# HG changeset patch # User IBBoard # Date 1483907358 0 # Node ID 720e9201dd98b99de1a0b0f79368c638d1cec215 # Parent e020410896ca4247a345f0719cf760ae005b7a7b Add basic grep diff -r e020410896ca -r 720e9201dd98 1-Ruby/day2-grep.rb --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/1-Ruby/day2-grep.rb Sun Jan 08 20:29:18 2017 +0000 @@ -0,0 +1,17 @@ +#! /usr/bin/env ruby + +if ARGV.length != 2 + abort("Incorrect arguments - pass a single file and a pattern") +elsif not File.exist? (ARGV[0]) + abort ("File #{ARGV[0]} did not exist") +end + +file = ARGV[0] +pattern = Regexp.new(ARGV[1]) +i = 0 +File.open(file, "r").each_line do |line| + i+= 1 + if line =~ pattern then + puts "#{i} #{line}" + end +end \ No newline at end of file