[Tutor] Grep equiv.

Ben Beuchler insyte@emt-p.org
Thu, 31 Aug 2000 16:36:52 -0500


On Thu, Aug 31, 2000 at 11:23:10AM -0500, Wes Bateman wrote:

> Still not able to get exactly what I want.  Really a result exactly like
> grep does is what I'm looking for.  I want to examine a file, line by
> line, and return the lines that contain a string that I was searching
> for.  To do this using string.find, I tried the following.  This searches
> for the string 'sdb' in a file, and I only want it to kick back the lines
> that contain the string.

I had better (easier to manage) results with the string.count()
function:

cat > testing
line one
line two
line one
line three
line four
^D


petra:~$ python
Python 1.5.2 (#2, Jun 29 2000, 21:39:35)  [GCC egcs-2.91.66
19990314/Linux (egcs- on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import string
>>> thefile = open('testing')
>>> contents = thefile.readlines()
>>> 
>>> for each in contents:
...     if string.count(each, 'one'):
...         print each
... 
line one

line one

>>> 


-- 
Ben Beuchler                                         insyte@bitstream.net
MAILER-DAEMON                                         (612) 321-9290 x101
Bitstream Underground                                   www.bitstream.net