You are here

Exercises

24 February, 2015 - 10:37

Exercise 11.1 Write a simple program to simulate the operation of the the grep command on Unix. Ask the user to enter a regular expression and count the number of lines that matched the regular expression:

$ python grep.pyEnter a regular expression: ˆAuthormbox.txt had 1798 lines that matched ˆAuthor

$ python grep.pyEnter a regular expression: ˆXmbox.txt had 14368 lines that matched ˆX-

$ python grep.pyEnter a regular expression: java$mbox.txt had 4218 lines that matched java$

Exercise 11.2 Write a program to look for lines of the form

New Revision: 39772

And extract the number from each of the lines using a regular expression and the findall() method. Compute the average of the numbers and print out the average.

Enter file:mbox.txt38549.7949721

Enter file:mbox-short.txt39756.9259259