473,378 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

grep

What's the standard replacement for the obsolete grep module?
Thanks,
Alan Isaac
Oct 25 '05 #1
3 3270
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: sf | last post by:
Just started thinking about learning python. Is there any place where I can get some free examples, especially for following kind of problem ( it must be trivial for those using python) I have...
2
by: John E. Jardine | last post by:
Hi, Problem: Executing 's///' has a side effect on grep null string matching. If line 62, the substitution, is executed the last two values returned by grep and printed on lines 68, 69 are...
1
by: Al Belden | last post by:
Hi all, I've been working on a problem that I thought might be of interest: I'm trying to replace some korn shell scripts that search source code files with perl scripts to gain certain features...
4
by: agarwalpiyush | last post by:
Hello, I am going nuts with trying to get the following to work: This is what I intend to do: I have a line in /etc/syslog.conf which I need to delete based on ip-address provided to me in a...
4
by: js | last post by:
Just my curiosity. Can python beats perl at speed of grep-like processing? $ wget http://www.gutenberg.org/files/7999/7999-h.zip $ unzip 7999-h.zip $ cd 7999-h $ cat *.htm bigfile $ du -h...
15
by: tereglow | last post by:
Hello all, I come from a shell/perl background and have just to learn python. To start with, I'm trying to obtain system information from a Linux server using the /proc FS. For example, in...
3
by: MyMarlboro | last post by:
print Dumper(@data); _____________ $VAR1= '2008,Car,Medium1,5000'; $VAR2= '2008,Car,Medium2,5000'; $VAR3= '2007,Plane1,Medium1,20000'; $VAR4= '2006,Boat,Small1,20000'; $VAR5=...
13
by: Anton Slesarev | last post by:
I've read great paper about generators: http://www.dabeaz.com/generators/index.html Author say that it's easy to write analog of common linux tools such as awk,grep etc. He say that performance...
47
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000):...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.