A ruby version of grep?

I need a grep variant in pure ruby.

Like the unix grep.

It does not have to support many fancy things…

Colours (so the result can be colourized) and recursive searching of
files.

The rest does not matter.

Has anyone written such a class? I am asking before I write my own
version of grep.

On Nov 1, 2011, at 2:19 PM, Marc H. wrote:

Has anyone written such a class? I am asking before I write my own
version of grep.

Try ack:

http://betterthangrep.com/

On Wednesday 02 November 2011 06:19:20 Marc H. wrote:

Has anyone written such a class? I am asking before I write my own
version of grep.

There’s rak [1], although I don’t know exactly what kind of colour
support you
need. If you use ruby 1.9, you may want to apply the patch described in
the
pull request in rak’s github page. Without it, it’ll stop at the first
non-
ASCII character it finds.

Stefano

[1] https://github.com/danlucraft/rak

Hey Eric,

ack is written purely in Perl, and takes advantage of
the power of Perl’s regular expressions

I am using Ruby, not Perl.

If I would use ack, in perl, then I could use grep, in C,
which is the same difference - both is not Ruby.

The reason I need it, in ruby, is because I want to connect
it to my pseudo-shell which is written in pure Ruby right now.

On Linux this may be irrelevant but my pseudo-shell shall
work on Windows too without any further downloads.

There’s rak [1], although I don’t know exactly what kind
of colour support you need.

Basically just ANSI Colour code.

You know, the classical XTERM escape codes:

BLACK = “\e[0;30m”
RED = “\e[0;31m”

If you use ruby 1.9, you may want to apply the
patch described in the pull request in rak’s
github page. Without it, it’ll stop at the
first non-ASCII character it finds.

Ok thanks! I checked it out, it looks really neat so far.
Also uses colours.

I think it is 98% what I am looking for … I will play
with it intensively. :slight_smile:

Does your grep implementation understand --color key?

$ grep --color=always MemTotal /proc/meminfo
^[[01;31m^[[KMemTotal^[[m^[[K: 8113764 kB

http://www.incava.org/projects/glark

not sure how active it is.

martin

Sorry about repetition but GNU grep support recursive search too:

-R, -r, --recursive
Read all files under each directory, recursively; this is
equivalent to the -d recurse option.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs