[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Grep via Ruby

ralf

2/7/2006 1:07:00 PM

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??

Thanks in advance
Ralf Müller

12 Answers

Robert Klemme

2/7/2006 1:15:00 PM

0

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.

14:14:49 [~]: grep -i foo <<EOF
> foo
> FOO
> EOF
foo
FOO
14:14:58 [~]:

robert

Fritz Heinrichmeyer

2/7/2006 1:19:00 PM

0


>
> DOES match. Does anyone see the bug?
/<something>/i is syntactic shugar for

Regexp.new(<something>, flag-for-case-insensitive-i-cannot-remember)

what you do is

Regexp.new("/<something>/i")

this is not what you want

--
Mit freundlichen Grüßen
Fritz Heinrichmeyer FernUniversität, LG ES, 58084 Hagen (Germany)
tel:+49 2331/987-1166 fax:987-355

Jeff Schwab

2/7/2006 1:58:00 PM

0

ralf wrote:

> I'd like to have a "grep" with case-insensitive match. normal grep does
> not have this functionality (afaik), but ruby does.

Are you talking about Enumerable#grep or the grep command-line tool?

ralf

2/7/2006 2:09:00 PM

0

I meant the command-line tool. I thought, that 'grep' should get the
'ignore-case' flag by the pattern like in ruby/perl and didn't thaught
about the command-line options. :((
Sorry for this so-damn-simple question.

regards
Ralf

Edwin van Leeuwen

2/7/2006 2:14:00 PM

0

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

Grep does have this functionality with the i switch.
grep -i

That being said it is ofcourse always fun to write your own script for
this :)

--
Posted via http://www.ruby-....


James Gray

2/7/2006 2:31:00 PM

0

On Feb 7, 2006, at 7:08 AM, ralf wrote:

> I'd like to have a "grep" with case-insensitive match.

You can turn options on/off inside a pattern. For example, here is a
pattern that matches the word file, regardless of case:

(?i:file)

> Maybe this can be done a lot easier by using ARGF??

Yes it can. Your input loop can be replaced with:

ARGF.grep(pattern) do |line|
puts "#{ARGF.filename} #{ARGF.lineno}: #{line}"
end

Hope that helps.

James Edward Gray II



Logan Capaldo

2/7/2006 7:14:00 PM

0


On Feb 7, 2006, at 9:30 AM, James Edward Gray II wrote:

> On Feb 7, 2006, at 7:08 AM, ralf wrote:
>
>> I'd like to have a "grep" with case-insensitive match.
>
> You can turn options on/off inside a pattern. For example, here is
> a pattern that matches the word file, regardless of case:
>
> (?i:file)
>
>> Maybe this can be done a lot easier by using ARGF??
>
> Yes it can. Your input loop can be replaced with:
>
> ARGF.grep(pattern) do |line|
> puts "#{ARGF.filename} #{ARGF.lineno}: #{line}"
> end
>
> Hope that helps.
>
> James Edward Gray II
>
>

Minor suggestion, line is going to end in an nl anyway. I would use
print, unless you want all that extra whitespace in the output.


James Gray

2/7/2006 7:40:00 PM

0

On Feb 7, 2006, at 1:13 PM, Logan Capaldo wrote:

> Minor suggestion, line is going to end in an nl anyway. I would use
> print, unless you want all that extra whitespace in the output.

puts() adds a newline character only if the string didn't already end
in one.

James Edward Gray II


William James

2/7/2006 8:40:00 PM

0

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

Logan Capaldo

2/7/2006 8:57:00 PM

0


On Feb 7, 2006, at 2:39 PM, James Edward Gray II wrote:

> On Feb 7, 2006, at 1:13 PM, Logan Capaldo wrote:
>
>> Minor suggestion, line is going to end in an nl anyway. I would
>> use print, unless you want all that extra whitespace in the output.
>
> puts() adds a newline character only if the string didn't already
> end in one.
>
> James Edward Gray II
>

Egads! Surely you jest!

% irb
irb(main):001:0> puts "Hello\n"
Hello
=> nil
irb(main):002:0> puts "Hello\n "
Hello
=> nil


You jesteth not. And to think this whole time I've been typing print
when I could have been typing puts.