Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > grep

Reply
 
 
David Isaac
Guest
Posts: n/a
 
      10-25-2005
What's the standard replacement for the obsolete grep module?
Thanks,
Alan Isaac


 
Reply With Quote
 
 
 
 
marduk
Guest
Posts: n/a
 
      10-25-2005
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


 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-25-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/...reconvert.html
http://effbot.org/librarybook/reconvert.htm



 
Reply With Quote
 
David Isaac
Guest
Posts: n/a
 
      10-25-2005
"Fredrik Lundh" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unix commands implemented ? (grep, wc, cat...) Spendius Java 2 12-13-2004 01:58 PM
Grep Hans Bijvoet Java 5 11-20-2004 01:52 AM
perl vs Unix grep Al Belden Perl 1 07-07-2004 05:58 AM
s/// has apparent side effect on grep() John E. Jardine Perl 2 04-13-2004 08:45 PM
Pattern matching help! grep emails from file! danpres2k Perl 3 08-25-2003 02:47 PM



Advertisments