Program: tcgrep

This program is a Perl rewrite of the Unix grep program. Although it runs slower than C versions (especially the GNU greps), it offers many more features.

The first, and perhaps most important, feature is that it runs anywhere Perl does. Other enhancements are that it can ignore anything that’s not a plain text file, automatically expand compressed or gzip ped files, recurse down directories, search complete paragraphs or user-defined records, look in younger files before older ones, and add underlining or highlighting of matches. It also supports both the -c option to indicate a count of matching records as well as -C for a count of matching patterns when there could be more than one per record.

This program uses gzcat or zcat to decompress compressed files, so this feature is unavailable on systems without these programs and systems without the ability to run external programs (such as the Macintosh).

Run the program with no arguments for a usage message (see the usage subroutine in the following code). This command line recursively and case-insensitively greps every file in ~/mail for mail messages from someone called “kate”, reporting the filenames that contained matches.

% tcgrep -ril '^From: .*kate' ~/mail

The program is shown in Example 6.14.

Example 6-14. tcgrep

#!/usr/bin/perl -w # tcgrep: tom christiansen's rewrite of grep # v1.0: Thu Sep 30 16:24:43 MDT 1993 # v1.1: Fri Oct 1 08:33:43 MDT 1993 # v1.2: Fri Jul 26 13:37:02 CDT 1996 # v1.3: Sat Aug 30 14:21:47 ...

Get Perl Cookbook 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.