-
Notifications
You must be signed in to change notification settings - Fork 0
/
grep3.py
executable file
·41 lines (30 loc) · 1.5 KB
/
grep3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3
import sys, optparse
def LOG(text):
print(text, file=sys.stderr)
def grep(string, inputFile, ignoreCase=False):
for line in open(inputFile).readlines():
line = line.rstrip("\n")
if not ignoreCase and string in line:
print(line)
LOG("matched with case %s in line %s" % (string, line))
elif ignoreCase and string.lower() in line.lower():
print(line)
LOG("matched w/out case %s in line %s" % (string, line))
else:
LOG("no match for %s in line %s" % (string, line))
cmdLineParser = optparse.OptionParser()
#cmdLineParser.add_option("-n", action="store", type="int", dest="ndays", default = 0, help="Number of days to process")
#cmdLineParser.add_option("-d", action="store", type="string", dest="date", default=datetime.datetime.today().strftime("%Y%m%d"), help="(YYMMDD) The date for which you want the stats. Default is today")
cmdLineParser.add_option("-i", action="store_true", dest="ignore_case", default = False, help = "ignore case")
#parse the command line arguments
(options, args) = cmdLineParser.parse_args(sys.argv)
string = args[1] #this is the string to search for
inputFile = args[2] #this is the file to search into
grep(string, inputFile, options.ignore_case)
grep("hero", "novel.txt")
grep("hero", "novel.txt", False)
grep("hero", "novel.txt", True)
grep(inputFile="novel.txt", string="hero")
grep(inputFile="novel.txt", string="hero", True)
grep(string="hero", inputFile="novel.txt", ignoreCase=True)