home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 27.12 Make Custom grep Commands (etc.) with perl Chapter 27
Searching Through Files
Next: 27.14 Compound Searches
 

27.13 More grep-like Programs Written in Perl

[Article 27.12 gave a sense of how easy it might be to write custom search programs in Perl...but until you learn the language, you can't rip these off whenever you find yourself in need. This article describes a few more custom greps written in Perl. It doesn't show the scripts themselves, just how to use them. If you like them, they are on the disc and the online archive. -TOR ]

tgrep
The tgrep program grep s only those files containing textual data. It's useful in a directory that has mixed binary and textual files, when the filenames aren't a sufficient clue to the nature of the file. tgrep has one option, -l , which causes it to list the files containing the pattern rather than listing the lines containing the pattern.
pipegrep
The pipegrep program grep s the output of a series of commands. The difficulty with doing this using the normal grep program is that you lose track of which file was being processed. This program prints out the command it was executing at the time, including the filename. The command, which is a single argument, will be executed once for each file in the list. If you give the string {} anywhere in the command, the filename will be substituted at that point. Otherwise the filename will be added on to the end of the command. This program has one option, -l , which causes it to list the files containing the pattern. For example [ nm is a programmers' utility that prints symbol name lists- JP  ]:

$ 

cd /usr/lib


$ 

pipegrep 'sys_nerr' nm lib*.a


nm /usr/lib/libX11.a |:          U _sys_nerr
nm /usr/lib/libXaw.a |:          U _sys_nerr
nm /usr/lib/libXaw.a |:          U _sys_nerr
nm /usr/lib/libc.a |:            U _sys_nerr
           .
           .
           .

cgrep
The cgrep program grep s for a pattern in the specified files, and prints out that line with several lines of surrounding context. This context grep script lets you specify how many lines of context you want if you want more or less than the default. For example:

$ 

cgrep -3 



pattern files

would give you three lines of context above and below the matching line. Each occurrence is separated from the next by a short horizontal line ( ----- ).

- LW , RS , TC from O'Reilly & Associates' Programming Perl


Previous: 27.12 Make Custom grep Commands (etc.) with perl UNIX Power Tools Next: 27.14 Compound Searches
27.12 Make Custom grep Commands (etc.) with perl Book Index 27.14 Compound Searches

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System