Advertisement
Guest User

Untitled

a guest
Dec 18th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import re
  2.  
  3.  
  4. def main():
  5. s=input()
  6. s=s+' '
  7. if (s.find('grep')!=-1):
  8. start=s.find('\'')+1
  9. end=s.rfind('\'')
  10. st=s[start:end]
  11. end=end+1
  12. while (s[end] == ' '):
  13. end = end + 1
  14. start=end
  15. while (s[end] != ' '):
  16. end = end + 1
  17. filename=s[start:end]
  18. try:
  19. file = open(filename, 'r', encoding='utf8')
  20. except PermissionError:
  21. return
  22. if ((s.find('-i')!=-1)and (s.find('-m')!=-1)and (s.find('-n')!=-1)):
  23. num = int(s[s.find('-m') + 3])
  24. count = 0
  25. linecount = 1
  26. for line in file:
  27. if ((re.search(st.lower(), line.lower()) != None) and (count < num)):
  28. strcount = str(linecount)
  29. print(strcount + ':' + line)
  30. count = count + 1
  31. linecount = linecount + 1
  32. elif ((s.find('-i')!=-1)and (s.find('-m')!=-1)):
  33. num = int(s[s.find('-m') + 3])
  34. count = 0
  35. for line in file:
  36. if ((re.search(st.lower(), line.lower()) != None) and (count < num)):
  37. print(line)
  38. count = count + 1
  39. elif ((s.find('-m')!=-1)and (s.find('-n')!=-1)):
  40. num = int(s[s.find('-m') + 3])
  41. count = 0
  42. linecount = 1
  43. for line in file:
  44. if ((re.search(st, line) != None) and (count < num)):
  45. strcount = str(linecount)
  46. print(strcount + ':' + line)
  47. count = count + 1
  48. linecount = linecount + 1
  49. elif ((s.find('-i')!=-1)and (s.find('-n')!=-1)):
  50. linecount = 1
  51. for line in file:
  52. if (re.search(st.lower(), line.lower()) != None):
  53. strcount = str(linecount)
  54. print(strcount + ':' + line)
  55. linecount = linecount + 1
  56. elif (s.find('-i')!=-1):
  57. for line in file:
  58. if (re.search(st.lower(), line.lower()) != None):
  59. print(line)
  60. elif (s.find('-m')!=-1):
  61. num=int(s[s.find('-m')+3])
  62. count=0
  63. for line in file:
  64. if ((re.search(st,line)!=None)and(count<num)):
  65. print(line)
  66. count=count+1
  67. elif (s.find('-n')!=-1):
  68. linecount=1
  69. for line in file:
  70. if (re.search(st,line)!=None):
  71. strcount=str(linecount)
  72. print(strcount+':'+line)
  73. linecount = linecount + 1
  74. else:
  75. for line in file:
  76. if (re.search(st,line)!=None):
  77. print(line)
  78.  
  79. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement