Warning

This book is new. If you'd like to go through it, then join the Learn Code Forum to get help while you attempt it. I'm also looking for feedback on the difficulty of the projects. If you don't like public forums, then email help@learncodethehardway.org to talk privately.

Exercise 7: grep

The find command should have been possible but a good challenge for 45 minutes. At this point you should be knocking down as many blockers to your start as you possibly can imagine. You may find that when you eliminate something your skill gets worse. For example, I used to walk to get a coffee before I started to work. This took me about 30 minutes and was very nice, but that's 30 minutes that many times turned into a few hours. I decided to stop doing that, but then my work suffered. Turns out I still needed coffee, so I bought a nice espresso machine and learned how to make my own lattes. Now I get up and make myself a latte and go do a bit of drawing, which gets me in the mode for doing creative work.

Not everything you do is an inneficiency, so be careful not to eliminate something simply because it takes up time. There are little rituals and personal habits that can get your brain ready to work. The trick is to not eliminate those but to instead make them easier to do before you start working.

You should also be getting a concept of time management during this first part of the book. Setting a time limit of 45 minutes will make it very clear that you have no idea how long you take to do something. With only 45 minutes you can't waste 30 getting your vim windows just right and organizing the perfect directory structure then crafting a brand new algorithm for sorting. You have to be frugal in what you implement and in what order you do the work.

A good way to tackle a project is to start with the simplest thing you can get up and running first. In the find example that was probably getting a file by a glob pattern. Someone with poor time management skills will immediately try to get the -exec argument working to prove they are an awesome coder, but -exec can't work without -name and is much more difficult to implement. The way to decide is to tell yourself that you want something you can use when you're done. If 45 minutes passes and you can use -exec but can't get files into it then how can you use it? If the same time passes and you've got a way to list files matching a name then you've done it. You have something in 45 minutes.

Keep working your list of blockers and evaluating how well you do with your start, but now start looking at time management. Strategize what you are going to implement so that if you ran out of time you'd have something you can use. They don't have to be complete features, but two useable features is better than 10 that don't work at all because you forgot the simplest thing they all need. Or worse, 10 that don't work because you bounced around half-implementing all of them so none of them work.

Exercise Challenge

You are now going to implement the grep command. As usual you should go read man grep and then play with it. The purpose of grep is to search for text pattern in files using regular expressions. You implemented find using the glob module, and the operation is similar but done inside a file rather than inside a directory. For example, if I want to search for the word "help" in my book I do this:

grep help *.rst

The command line arguments to grep are fairly simple. The difficult part is handling regular expressions, so you should rely on the re module. This module will enable you to load the contents of a file and then search through it for patterns people give you on the command line. Another hint is you will most likely want to load the whole file using readlines rather than using read. Most of the options to grep work better that way even though it is less efficient.

You may also want to briefly skip ahead to Exercise 30 where I give an introduction to Regular Expressions.

Study Drills

  1. Are there special options to the re module that make it work more like grep?
  2. Could you turn your grep hack into a module that you then use in your find tool to add a grep feature?

Further Study

The re module is very important, so take the time to really study it and learn everything you can about it. We'll be using it and regular expressions in another part of the book.

Pre-order Learn More Python The Hard Way

When you pre-order Learn More Python The Hard Way, you'll receive the Python 3 Edition as it's being created. All files are DRM free and you can download them to your computer for offline viewing. Digital Download Only! You do not get a physical book.

$29.99

Pre-Order From Zed

Or, you can read Learn More Python the Hard Way for free right here, video lectures not included.

Other Buying Options

Other buying options coming soon.

No content available for this exercise. You can view all available downloads at your customer account page.