Gennady Bystritsky
2/7/2006 8:48:00 PM
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? ;-)
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
>
>
>