Grep via Ruby

Sorry if it was mentioned already (and not to discourage the OP, of
course), but what's wrong with "grep -i"? Isn't it case-insensitive
enough? :wink:

Gennady.

路路路

-----Original Message-----
From: William James [mailto:w_a_x_man@yahoo.com]
Sent: Tuesday, February 07, 2006 12:43
To: ruby-talk ML
Subject: Re: Grep via Ruby

ralf wrote:
> Hi,
> I'd like to have a "grep" with case-insensitive match. normal grep
> does not have this functionality (afaik), but ruby does. So
this is,
> what i
> wrote:
>
> cat rgrep:
> #!/usr/bin/env ruby
> # Grep with full regexp-functionality via ruby
>
> if ARGV.shift == "-p"
> pattern = Regexp.new(ARGV.shift)
> else
> puts "Please give me a pattern with the '-p' option"
> exit
> end
> ARGV.each do |filename|
> File.open(filename) do |file|
> file.each do |line|
> puts "#{filename} #{file.lineno.to_s}: #{line}" if
> pattern.match(line)
> end
> end
> end
>
> Using it via: rgrep -p '/delete /i' *.php does not match
anything, but
> this #!/usr/bin/env ruby # Grep with full regexp-functionality via
> ruby
>
> if ARGV.shift == "-p"
> pattern = Regexp.new(ARGV.shift)
> else
> puts "Please give me a pattern with the '-p' option"
> exit
> end
> ARGV.each do |filename|
> File.open(filename) do |file|
> file.each do |line|
> puts "#{filename} #{file.lineno.to_s}: #{line}" if /delete
> /i.match(line)
> end
> end
> end
>
> DOES match. Does anyone see the bug? Maybe this can be done a lot
> easier by using ARGF??

if ARGV.shift == "-p"
  pattern = Regexp.new(ARGV.shift,Regexp::IGNORECASE)
else
  puts "Please give me a pattern with the '-p' option"
  exit
end

puts "#{$FILENAME} #{$.}: #{$_}" if $_ =~ pattern while gets

There's always glark [glark.sourceforge.net]

martin

路路路

Gennady Bystritsky <Gennady.Bystritsky@quest.com> wrote:

Sorry if it was mentioned already (and not to discourage the OP, of
course), but what's wrong with "grep -i"? Isn't it case-insensitive
enough? :wink: