Current Issue


Table of contents

CD-ROM

Sys Admin and The Perl Journal CD-ROM version 12.0

Version 12.0 delivers every issue of Sys Admin from 1992 through 2006 and every
issue of The Perl Journal from 1996-2002 in one convenient CD-ROM!

Order now!

Sys Admin Magazine > Archives > 2001 > June 2001

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

				  



MarketPlace

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.

Villanova University Six Sigma & IT Certificate Programs
100% Online programs in Six Sigma, IS Security, CISSP Prep, Business Analysis, Proj. Mgmt. and more!

Workflow Enabled Help Desk & IT Service Management
Automate service desk activities and integrate processes across IT. Learn more here.

Flowcharts from C/C++ code -- Free trial download
Understand C/C++ code in less time. A new team member ? Inherited legacy code ? Get up to speed faster with Crystal Flow for C/C++. Code-formatting improves readability. Flowcharts are integrated with code browser. Export flowcharts to Visio.

Wanna see your ad here?