All of the various grep -like utilities perform pretty much the same function, with minor differences - they search for a specified pattern in some or all of a file, and then display that pattern with varying amounts of surrounding context.
For example, to search for a pattern in the header of a Usenet message:
perl -ne 'exit if (/^$/); print if (/
[This works because
To do a search for a pattern and print the paragraphs in which it appears:
perl -ne '$/ = "\n\n"; print if (/
[This assumes that paragraphs are delimited by a double linefeed - that is, a blank line. You'd have to adjust this script for a troff or TeX document where paragraphs are separated by special codes. -TOR ]
Searching through files is one of Perl's strengths, but certainly
not its only strength. Perl encompasses all of the functionality of
, and other UNIX utilities.
Furthermore, a Perl program to do something originally done with
one or more of these utilities is usually faster and easier to read
than the non-Perl solution.
[I agree that Perl is usually faster than a bunch of separate UNIX
utilities strung together by pipes and temporary files.
It also beats many utilities running standalone.
But, in my experience, sed
beats Perl's running speed almost
That could be partly because I have a slow disk, and the 40-kbtye
binary takes less time to load than the 700-kbtye Perl 5
Make your own tests, and I'll make room for Jonathan's rebuttal in
the third edition of this book.