Name: [709] Jeff Rush
Member: 111 months
Authored: 15 videos
Description: Greetings. I'm the (former) Python Advocacy Coordinator and a strong supporter of screencasts. I'm also the organizer of the Dallas-Ft. Worth Pythoneers and was con chair for PyCon 2006 and 2007 in Dallas. I'm also an independent consultant and work in the areas of Python/Zope, embedded Linux s ...

Walkthru of a Simple Text Line Matcher (grep) [ID:368] (1/2)

in series: Python Source Walkthrough Series

video tutorial by Jeff Rush, added 09/07

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

Let's start with a simple program, one that reads lines of text from one or more files and prints out those lines that match a regular-expression pattern. We'll give it command-line options and then in successive reworkings in this talk have it underline the matches and accept multiple patterns any of which can match.

A simple introduction to Python programming for those just getting started.

#!/usr/bin/env python

"""GREP-Like Print Lines Matching One or More Given Patterns

   Search one or more named input files (standard
   input if no files are named, or the file name '-'
   is given) for lines containing a match to the
   given set of patterns.  If any one of them
   matches, print the line.

from optparse import OptionParser
import fileinput
import re

class MultiMatcher(object):
    """A Set of Searchable Regular Expression Patterns

       Accept one or more regular expressions such
       that if any one of them matches a line the
       first successful match is returned.

    def __init__(self, multipatterns):
        self.multipatterns = [ re.compile(pattern)
                               for pattern in multipatterns ]

    def search(self, line):
        for pattern in self.multipatterns:
            match =
            if match is not None:
                return match

def main():
    parser = OptionParser(
        usage="%prog [-u] -e PATTERN FILELIST",
        version="%prog 1.2")

    parser.add_option("-e", "--regexpr",
        action="append", dest="regexpr",
        help="pattern expression on which to match")

    parser.add_option("-u", "--underscore",
        action="store_true", dest="underscore", default=False,
        help="draw underscores under the matching text")

    options, args = parser.parse_args()

    if options.regexpr is None:
        parser.error("missing pattern against which to match")

    matcher = MultiMatcher(options.regexpr)

    for line in fileinput.input(args):
        if line.endswith("\n"):
            line = line[:-1]

        match =
        if match:
            start, end = match.start(), match.end()

            prefix = "%s:%d " % (
            print prefix + line

            if options.underscore:
                print prefix \
                      + " " * start \
                      + "-" * (end - start)

if __name__ == "__main__":

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 121 (since July 30th)
  • Plays in last week: 4
  • Published: 108 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>

All comments excluding tick-boxed quick-comments

46. Charlesea Tue, 20 Jul 2010 07:47

Thanks for the video!

45. Isabel Marant Wedge Trainers Ebay Wed, 23 Jun 2010 00:05

Great, thanks

44. Tue, 15 Jun 2010 16:22

Thanks a lot for your work!

43. Bogner Bequem Atmungsaktiv Wandern Lady Sun, 06 Jun 2010 09:16

Nice job. Several useful tricks I can use. Thank you!

42. mojammad khan Wed, 12 May 2010 13:24

Enjoyed it, like building from step to step. I think that's a good part of teaching.

41. Sat, 17 Apr 2010 12:07


The walkthru of the grep program was really helpful.

I didn't know about starting to read a python program from the bottom.

Chris Norway

40. michael kors handbags Fri, 16 Apr 2010 07:41

informative, yet needs to be broken down toward the basics. application , and variety. but this doesn't devalue the use of this video.

39. jasa SEO, Backlink, Blogwalking murah Wed, 31 Mar 2010 05:18

wonderful stuff. I know my RE's pretty well but this was just an exciting demo to watch unfold while illustrating python's power

38. coach factory online sale Tue, 16 Mar 2010 23:00

It is really helpful to see these samples, and hear you explain the code. I will try this myself some day.


37. Parajumpers Red Sports Sat, 27 Feb 2010 11:10

I've been trying to read python programs every now and then, but often the sheer complexity and the calls to libraries I didn't know made me give up before I reached the end. As a consequence, I appreciate the help a video like this one provides.

36. black true religion shirt Tue, 16 Feb 2010 14:09

Thanks! very helpful

Fun. Good. Useful and practical insight of classes, modules and parsing in 16 min.


33. marinir seo Thu, 21 Jan 2010 14:48

Beautifully presented

Thank you Jeff. Very nice and valuable.

31. ugg baby booties Mon, 11 Jan 2010 14:57

Nice Video. Good example on real life list comprehension.

30. anonymous Fri, 23 Oct 2009 12:00

I did find the video educational and did like the detail. One thing I would have liked to see was an example of the last modification to the program (where the class was added). thanks.

29. anonymous Sun, 04 Oct 2009 12:48

Thanks for the high quality tutorials on this site.

Really enjoyed the to-the-point nature of the video.

Really enjoyed the to-the-point nature of the video.

26. anonymous Thu, 24 Sep 2009 12:37

Hi Jeff, thanks for the video. That will help me if I need to do some command line parsing. It was just the kind of tutorial I'm looking for


25. anonymous Mon, 24 Aug 2009 16:59

very nice video

24. anonymous Thu, 13 Aug 2009 17:11

great work!

23. anonymous Wed, 05 Aug 2009 18:34

Thanks, I liked this video very much. How does optparse know a filename argument is not an regex? Is a user required to invoke the e flag for each regex he/she wants to search for?

Lots of new modules in use I never seen before. Great knowledge in my endeavors with Python. Thanks!

21. anonymous Tue, 28 Jul 2009 12:29

Good one!

...Music is the best.

20. anonymous Sat, 11 Jul 2009 14:45

Awesome! I think what you are doing is great! One bit of advice though, it's good to explain things throughly but sometimes you pause a lot and it seems a bit slow. Other than that, I love it. Do you happen to be able to do some C or C++ also??

19. anonymous Thu, 02 Jul 2009 18:27


18. anonymous Thu, 11 Jun 2009 12:15

Great vid, new to python and nice to learn a bit about a new module.


17. anonymous Sat, 06 Jun 2009 07:39

I came from a Perl background, I went through your tutorial, it's brilliant. Especially at the end where you handle the multi regex as a class and there were minimal code changes on the main function.

Thanks and keep up the good work.

Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for Jeff

Learning Paths

This series lies on the following learning-paths. Learning-paths are a new initiative at Showmedo, to start structuring our content better. You can find out more here.



Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.