grep

Fredrik Lundh fredrik at pythonware.com
Tue Oct 25 03:51:31 EDT 2005


"marduk" wrote:

>> What's the standard replacement for the obsolete grep module?
>
> AFAIK there never was a "grep" module.  There does, however exist a
> deprecated "regex" module:

there was a "grep" module in 1.5.2 and earlier:

    http://effbot.org/librarybook/grep.htm

but it was removed in 2.0 (or somewhere around that time).

the new RE package doesn't support all the RE syntax variants used by that
module, but if you're willing to update your patterns [1] to use the new syntax,
you can replace grep with some variation of:

    import re, glob

    def grep(pattern, *files):
        search = re.compile(pattern).search
        for file in files:
            for index, line in enumerate(open(file)):
                if search(line):
                    print ":".join((file, str(index+1), line[:-1]))

    grep("grep", *glob.glob("*.py"))

(tweak as necessary)

</F>

1) to some extent, the reconvert module might be able to help you with the con-
version:

    http://www.python.org/dev/doc/devel/lib/module-reconvert.html
    http://effbot.org/librarybook/reconvert.htm 






More information about the Python-list mailing list