annotate 1-Ruby/day2-grep.rb @ 29:9c7af76fdbd0
Print isn't PrinLn, so add some \n characters to the output
author |
IBBoard <dev@ibboard.co.uk> |
date |
Wed, 06 Sep 2017 18:36:02 +0100 |
parents |
720e9201dd98 |
children |
|
rev |
line source |
23
|
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 |