Skip to content

Instantly share code, notes, and snippets.

@wynemo
Created February 20, 2012 14:42
Show Gist options
  • Save wynemo/1869519 to your computer and use it in GitHub Desktop.
Save wynemo/1869519 to your computer and use it in GitHub Desktop.
find text in file
#!/usr/bin/env python
#coding:utf-8
#use example:
# find|python /tmp/myfind.py sth
# on cygwin
# find|python "C:\Users\user1\findtext\myfind.py" sth
import re
import sys
import os
#import fileinput
#for line in fileinput.input():
# pass
l1 = sys.stdin.readlines()
for each in l1:
path1 = each.replace('\n','').replace('\r','')
_path1 = os.path.abspath(path1)
if os.path.isfile(_path1):
f1 = None
try:
f1 = open(_path1,'r')
s1 = f1.read()
o1 = re.search(sys.argv[1],s1,re.I|re.S)#not re.X for verbose,we need space
if o1:
print _path1
finally:
if f1:
f1.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment