A Powerful Search
Tool for ASCII Files
Koos Pol
Sometimes the main UNIX principle of combining small tools to accomplish complex
tasks just isn't enough. There are times that you just need more. A striking
example is the task of searching through ASCII files. This can be any sort of
file: C programs, error logs, HTML files, etc. If you need to find a specific
string, it is usually sufficient to grep through the file and view the
results. Combined with find, you can get a long way. For example, if
you need to dig through your HTML files and find the ones that have obsolete
links to your old department, you may want to run something like:
find /usr/local/web -name "*.html" -print |
while read F; do echo "**** $F";
grep http://intranet.mycompany.com/olddepartment $F; done
If your queries get a bit more complicated, you may still get by using egrep
instead of grep, but you will run out of steam very soon. Besides that,
you really don't want to learn all the egrep options if they can be different
on any operating system. So what's the alternative? This is a perfect challenge
for Perl regular expressions: they can be extremely powerful and are the same
for all UNIXes that run Perl. So, what if we could rewrite the monster above in
something more attractive, such as:
find.pl http://intranet.mycompany.com/olddepartment "/usr/local/web/*.html"
We obviously need to combine find and Perl's regular expressions in one
script. If we can do that, we really have a powerful tool for searching files.
Here are a few more examples:
Look for all your HTML files with images on remote servers:
find.p
|