grep

D

David Isaac

What's the standard replacement for the obsolete grep module?
Thanks,
Alan Isaac
 
M

marduk

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:
__main__:1: DeprecationWarning: the regex module is deprecated; please
use the re module
 
F

Fredrik Lundh

marduk said:
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
 
D

David Isaac

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top