Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- def main():
- s=input()
- s=s+' '
- if (s.find('grep')!=-1):
- start=s.find('\'')+1
- end=s.rfind('\'')
- st=s[start:end]
- end=end+1
- while (s[end] == ' '):
- end = end + 1
- start=end
- while (s[end] != ' '):
- end = end + 1
- filename=s[start:end]
- try:
- file = open(filename, 'r', encoding='utf8')
- except PermissionError:
- return
- if ((s.find('-i')!=-1)and (s.find('-m')!=-1)and (s.find('-n')!=-1)):
- num = int(s[s.find('-m') + 3])
- count = 0
- linecount = 1
- for line in file:
- if ((re.search(st.lower(), line.lower()) != None) and (count < num)):
- strcount = str(linecount)
- print(strcount + ':' + line)
- count = count + 1
- linecount = linecount + 1
- elif ((s.find('-i')!=-1)and (s.find('-m')!=-1)):
- num = int(s[s.find('-m') + 3])
- count = 0
- for line in file:
- if ((re.search(st.lower(), line.lower()) != None) and (count < num)):
- print(line)
- count = count + 1
- elif ((s.find('-m')!=-1)and (s.find('-n')!=-1)):
- num = int(s[s.find('-m') + 3])
- count = 0
- linecount = 1
- for line in file:
- if ((re.search(st, line) != None) and (count < num)):
- strcount = str(linecount)
- print(strcount + ':' + line)
- count = count + 1
- linecount = linecount + 1
- elif ((s.find('-i')!=-1)and (s.find('-n')!=-1)):
- linecount = 1
- for line in file:
- if (re.search(st.lower(), line.lower()) != None):
- strcount = str(linecount)
- print(strcount + ':' + line)
- linecount = linecount + 1
- elif (s.find('-i')!=-1):
- for line in file:
- if (re.search(st.lower(), line.lower()) != None):
- print(line)
- elif (s.find('-m')!=-1):
- num=int(s[s.find('-m')+3])
- count=0
- for line in file:
- if ((re.search(st,line)!=None)and(count<num)):
- print(line)
- count=count+1
- elif (s.find('-n')!=-1):
- linecount=1
- for line in file:
- if (re.search(st,line)!=None):
- strcount=str(linecount)
- print(strcount+':'+line)
- linecount = linecount + 1
- else:
- for line in file:
- if (re.search(st,line)!=None):
- print(line)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement