diff options
Diffstat (limited to 'ruby/grep.rb')
-rwxr-xr-x | ruby/grep.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/ruby/grep.rb b/ruby/grep.rb new file mode 100755 index 0000000..61d60b0 --- /dev/null +++ b/ruby/grep.rb @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby + +exit 1 if ARGV.size < 2 + +pattern = ARGV[0] + +ARGV[1..-1].each do |fname| + unless File.file?(fname) + puts "File #{fname} does not exist or is a directory" + exit 1 + end + File.open(fname, 'r') do |f| + f.each_line do |line| + puts "#{f.lineno}:\t#{line}" if line =~ /#{pattern}/ + end + end +end |