By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,399 Members | 886 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,399 IT Pros & Developers. It's quick & easy.

grep

P: n/a
What's the standard replacement for the obsolete grep module?
Thanks,
Alan Isaac
Oct 25 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
Oct 25 '05 #2

P: n/a
"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

Oct 25 '05 #3

P: n/a
"Fredrik Lundh" <fr*****@pythonware.com> 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
Oct 25 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.