[Tutor] Grep equiv.

Burchill, Scott B. sburch@ordway.org
Thu, 31 Aug 2000 16:43:32 -0500


> -----Original Message-----
> From: Wes Bateman [mailto:wbateman@epicrealm.com]
> 
> 	#!/usr/bin/python
> 	
> 	import sys
> 	import string
> 	import re
> 	
> 	filename = sys.argv[1]
> 	
> 	file = open(filename)
> 	for line in file.readlines():
> 		result = string.find(line,'sdb')
> 		if result != '-1' :
> 			print line
> 	file.close()	
> 


Wes,

     You are so close.  You are using the for loop to check each line in the
file and that is working great.  It's the "if result" line where the problem
is.

     string.find returns a *number* and you are checking it against a
string, '-1'.  There are two possible solutions :

> 		result = str(string.find(line,'sdb'))
> 		if result != '-1' :

-or-

> 		result = string.find(line,'sdb')
> 		if result != -1 :

Either of these will make your script pick the correct lines to print.

sbb

PS sorry for the previous incomplete posting :)