grep

Discussion in 'Python' started by David Isaac, Oct 25, 2005.

  1. David Isaac

    David Isaac Guest

    What's the standard replacement for the obsolete grep module?
    Thanks,
    Alan Isaac
     
    David Isaac, Oct 25, 2005
    #1
    1. Advertising

  2. David Isaac

    marduk Guest

    On Tue, 2005-10-25 at 06:45 +0000, David Isaac 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:

    >>> import regex

    __main__:1: DeprecationWarning: the regex module is deprecated; please
    use the re module
     
    marduk, Oct 25, 2005
    #2
    1. Advertising

  3. "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
     
    Fredrik Lundh, Oct 25, 2005
    #3
  4. David Isaac

    David Isaac Guest

    "Fredrik Lundh" <> wrote::
    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"))


    I was afraid the re module was the answer. ;-)
    Use of enumerate is a nice idea.
    Thanks.
    Alan
     
    David Isaac, Oct 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. danpres2k
    Replies:
    3
    Views:
    7,519
    danpres2k
    Aug 25, 2003
  2. John E. Jardine

    s/// has apparent side effect on grep()

    John E. Jardine, Apr 12, 2004, in forum: Perl
    Replies:
    2
    Views:
    475
    John Jardine
    Apr 13, 2004
  3. Al Belden

    perl vs Unix grep

    Al Belden, Jul 3, 2004, in forum: Perl
    Replies:
    1
    Views:
    5,263
    Giridhar Nandigam
    Jul 7, 2004
  4. Hans Bijvoet

    Grep

    Hans Bijvoet, Nov 19, 2004, in forum: Java
    Replies:
    5
    Views:
    854
    David Zimmerman
    Nov 20, 2004
  5. Spendius
    Replies:
    2
    Views:
    3,080
    Rogan Dawes
    Dec 13, 2004
Loading...

Share This Page