comparison 1-Ruby/day2-grep.rb @ 23:720e9201dd98

Add basic grep
author IBBoard <dev@ibboard.co.uk>
date Sun, 08 Jan 2017 20:29:18 +0000
parents
children
comparison
equal deleted inserted replaced
22:e020410896ca 23:720e9201dd98
1 #! /usr/bin/env ruby
2
3 if ARGV.length != 2
4 abort("Incorrect arguments - pass a single file and a pattern")
5 elsif not File.exist? (ARGV[0])
6 abort ("File #{ARGV[0]} did not exist")
7 end
8
9 file = ARGV[0]
10 pattern = Regexp.new(ARGV[1])
11 i = 0
12 File.open(file, "r").each_line do |line|
13 i+= 1
14 if line =~ pattern then
15 puts "#{i} #{line}"
16 end
17 end