-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrep.py
95 lines (80 loc) · 3.19 KB
/
grep.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Implementation of grep command using python script
"""
import os
import argparse
import re
import glob
def grep(char, files, dir_name):
try:
char_to_search = re.compile(r'.*{}.*'.format(char))
os.chdir(dir_name)
search_files = glob.glob(files)
for file in search_files:
with open(file, 'r', encoding='utf-8') as f:
contents = f.read()
matcher = char_to_search.findall(contents)
for match in matcher:
print(file, ':', match)
except Exception as e:
print(e)
def recursive(char, files, dir_name):
try:
char_to_search = re.compile(r'.*{}.*'.format(char))
for dir_path, dir_names, file_names in os.walk(dir_name):
search_files = glob.glob(os.path.join(dir_path, files))
for file in search_files:
with open(file, 'r', encoding='utf-8') as f:
contents = f.read()
matcher = char_to_search.findall(contents)
for match in matcher:
print(file, ':', match)
except Exception as e:
print(e)
def ignore_case_sensitive(char, files, dir_name):
try:
char_to_search = re.compile(r'.*{}.*'.format(char), re.I)
os.chdir(dir_name)
search_files = glob.glob(files)
for file in search_files:
with open(file, 'r', encoding='utf-8') as f:
contents = f.read()
matcher = char_to_search.findall(contents)
for match in matcher:
print(file, ':', match)
except Exception as e:
print(e)
def invert(char, files, dir_name):
try:
char_to_search = re.compile(r'.[^'+ char + '}].*')
os.chdir(dir_name)
search_files = glob.glob(files)
for file in search_files:
with open(file, 'r', encoding='utf-8') as f:
contents = f.read()
matcher = char_to_search.findall(contents)
for match in matcher:
print(file, ':', match)
except Exception as e:
print(e)
def main():
""" Main Function of the program """
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
parser.add_argument("search", nargs='?', help="The character to search", type=str)
parser.add_argument("files", nargs='?', help="The file pattern to search for", type=str)
parser.add_argument("path", nargs='?', help="The directory path to search in", type=str)
group.add_argument("-r", "--recursive", help="search the path to search in recursively", action="store_true")
group.add_argument("-i", "--ignorecase", help="ignore case sensitivity", action="store_true")
group.add_argument("-m", "--invertmatch", help="select non-matching lines", action="store_true")
args = parser.parse_args()
if args.recursive:
recursive(args.search, args.files, args.path)
elif args.ignorecase:
ignore_case_sensitive(args.search, args.files, args.path)
elif args.invertmatch:
invert(args.search, args.files, args.path)
else:
grep(args.search, args.files, args.path)
if __name__ == '__main__':
main()