Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 1-Ruby/day2-grep.rb @ 24:cd874e58dbc5
Add code from book
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Fri, 20 Jan 2017 20:59:01 +0000 |
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 |