Question: Exercise 1: Write a simple program to simulate the operation of the grep command on Unix. a. Ask the user to enter a regular expression
Exercise 1: Write a simple program to simulate the operation of the grep command on Unix.
a. Ask the user to enter a regular expression
b. Count the number of lines that matched the regular expression:
This is what I have done already, but I want to do the same thing without the input function
import re x = input('Enter a regular expression: ') # reading argument from user num_of_lines_matched = 0 # number of lines matched so far with open('mbox.txt', 'r') as file_input: for line in file_input: # finding the exact match same as grep line = re.findall("\\b" + x + "\\b", line) # if we found any exact match, we will increase count number for that line if len(line) > 0: num_of_lines_matched += 1 # print(line) # display final output print('mbox.txt had', num_of_lines_matched, 'lines that matched')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
