hi there got a couple of probs, say in my text file i have:
abase
abased
abasement
abasements
abases
--------
This coding below is meant to find a word in a file and print all the lines to the end of the file. But it doesnt, it only prints out my search term and not the rest of the file.
search_term = r'\b%s\b' % search_term
for line in open(f, 'r'):
if re.match(search_term, line):
if search_term in line:
print line,
Say i searched for abasement, i would like the output to be:
abasement
abasements
abases
----------
My final problem is, i would like to search a file a print the lines my search term is in and a number of lines befer and after the searchterm. If i searched the text example above with 'abasement' and i defined the number of lines to print either side as 1 my output would be:
abased
abasement
abasements
numb = ' the number of lines to print either side of the search line '
search_term = 'what i search'
f=open("file")
d={}
for n,line in enumerate(f):
d[n%numb]=line.rstrip()
if search_term in line:
for i in range(n+1,n+1+numb):
print d[i%numb]
for i in range(1,numb):
print f.next().rstrip()
But i get a i get a Typeerror: unsupported operand type(s) for %: 'int' and 'str' which occurs at: d[n%numb] = line.rstrip()
Help please?