[CS-FSLUG] I need a q'n'd shell script or something for batch file editing

Frank Bax fbax at sympatico.ca
Sun Oct 17 08:03:04 CDT 2004


At 11:32 PM 10/16/04, Timothy R. Butler wrote:

>Hi everyone,
>
>PROBLEM: Internet Explorer chokes on a line of code that works great in -- 
>apparently -- every other browser ever on the face of the earth.
>
>FIX: Switch the call for a stylesheet from using a relative path to an 
>absolute path.
>
>NEW PROBLEM: That change needs to be made in a very large number of files 
>on my client's site.
>
>What I need is to find a way to write a quick and dirty batch script that 
>will edit the right line in each file.
>
>Grep can find the line just fine. Here's a bit of its output:


The problem will be to put the file back over top of the original.  I think 
that will be a bit tricky in a shell script  - if you "cat" the input file, 
then it is still open when you want to replace it.

So we start with a shell script (zz) that identifies the files (this 
assumes they are all in current directory) - args are old and new strings:

#!/bin/sh
for DD in `grep -l $1 *`
do
./zf $DD $1 $2
done

where zf contains:

#!/usr/bin/perl
die "usage ./zf DOMAIN OLDIP NEWIP" unless ( $ARGV[2] ) ;
$FD=`cat $ARGV[0]`;
$FD=~ s/$ARGV[1]/$ARGV[2]/g;
print $FD;
open(OUT,">$ARGV[0]");
print OUT $FD ;
close(OUT);


I must credit my friend Doug for writing these scripts.  The original zf 
was written to update ip addresses in named files, so it had some code 
(which I removed) to update serial number as well. 





More information about the Christiansource mailing list