[pm-h] Quick, helpful example scripts
G. Wade Johnson
gwadej at anomaly.org
Wed Oct 23 07:40:42 PDT 2013
In my last message, I pointed out that I started using Perl in quick
little scripts that solved an annoying problem.
Here's one of those scripts that I have written and modified probably
hundreds of times over the years.
---------------
#!/usr/bin/perl
while(<>)
{
s/old, bad text/new, shiny text/g;
print;
}
---------------
Because I never keep them around for long, this would normally be
written to the file "doit.pl" and deleted after use.
To the more advanced Perl programmers: I realize there are easier ways
to do this. But, I can hand this to someone with little experience and
get them using (and changing) it in short order. Likewise, I would
normally do this with my editor in most circumstances, but hold that
thought for now.
This script would be run one of two ways:
perl doit.pl file_to_change.txt
or
perl -i.bak doit.pl file_to_change.txt
The first sends the changed version to standard out. I can then pipe it
to "less" or write it to a separate file to see if it has made the
changes I want.
When I'm happy with the changes, I'll run the second version when
changes the file "in place" (that's what the '-i' is for) and leaves
the original file with a ".bak" extension.
The really cool thing about this, if you haven't used it before is that
you can replace "file_to_change.txt" with any number of files and
modify them all in one pass. For example,
perl -i.bak doit.pl *.txt
I have seen this script (with the search and replace text changed) save
people dozens of hours of work.
How does it work?
-----------------
If you don't understand it immediately, here's a quick walk-through.
while(<>)
This line is a Perl magic incantation that says read each line from the
supplied file one at a time and execute the body of the loop on each
line.
{
Start a block of commands to execute.
s/old, bad text/new, shiny text/g;
Although regular expressions often get a reputation for being complex
and hard to understand. This is a good example of a really simple
substitution.
Every place that the script finds the text "old, bad text", replace it
with "new, shiny text". If you need a special character in the search
text, put a '\' in front of it. If you need a '/' character in either
the search or the replace part, put a '\' in front of it.
The 'g' at the end means substitute every instance of "old, bad text" on
each line.
print;
Print the modified line to the output.
}
Finish the block of commands to execute.
There are a few edge cases that this code won't handle. But, I have
found it to be more than adequate in the past. In one case, I happened
on a web developer at a previous job that needed to make about a dozen
changes like this to hundreds of files for a client's website. He
planned to stay at work all night to make this happen.
Including debugging mistakes in the search and replace strings, I
helped him completely finish this in a little over half an hour. More
importantly, he kept the script and used it in several cases later at
the same job.
Conclusion
----------
One of the great things about this kind of script is it is pretty quick
to iterate through a series of changes. Don't make complicated s///
commands at first, just do the simple ones that make your life easier.
G. Wade
--
I never let schooling get in the way of my education. -- Mark Twain
More information about the Houston
mailing list