Exercise: The Unix grep

As you get further along in this book, the exercises will present you with more and more useful tools. This exercise presents a stripped-down version of the Unix grep utility. The Unix grep—not to be confused with Perl's grep function, introduced in Hour 6, “Pattern Matching”—searches files for patterns. This exercise presents a utility that will prompt for a directory name and a pattern. Every file in that directory will be searched for that pattern, and lines matching that pattern will be printed.

In future exercises, this utility will be modified to search subdirectories (see Hour 15, “Finding Permanence”) and to take command-line arguments (see Hour 12, “Using Perl's Command-Line Tools”). Stay tuned for details.

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.