Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I'm new to perl and haven't y

Status
Not open for further replies.

richman

MIS
Joined
Jan 11, 2000
Messages
18
Location
US
Hi,
I'm new to perl and haven't yet figured out how to rewrite an existing file. I have a text book that came with dozens of example scripts all pointing to /usr/bin/perl.
The path on our unix machine is /opt/local/bin/perl/bin.
I would like to create a script that changes the path, posted in the beginning of each script, from the existing path to the correct path (rewriting to the same/new filename). The alternative (ugh!?) is editting each one till I can find that chapter - unlikely!
I created a command
s/#!\/usr\/bin\/perl/\/#!\/opt\/local\/perl\/bin\/perl/
that will perform the substitution (looks just like sed).
I have also been able to read/echo multiple perl (*.pl) files passed from the command line but am unclear on how to edit and rewrite each line ($ln) and file. These files, as they were supplied, reside in multiple subdirectories (e.g., ch01, ch02,...). I would like the change to traverse these 'ch'apter subdirectories if possible.
Thanks for listening... [sig][/sig]
 
#Usage filename.pl *.pl

$SearchString = "search";
$ReplaceString = "replace";

while ($FileString = shift){
@Files = `dir *$FileString* /s /b /n`; # Change this to equivalent in linux
foreach $i (@Files) {
chop($i);
if (-e $i) {
open(FILE,$i) || ($FileExists = 0);
@FileData = <FILE>;
close(FILE);

#Put your search and replace here

$FileCount++ if @Matches;
foreach $j (@Matches) { $hits++; print &quot;$i\:\:$j&quot;;}
}
}
}
print &quot;Total matches : $hits\n&quot;;
print &quot;Total number of files matched: $FileCount \n&quot;;
exit;


I am in a hurry, just fix the above script and that should do the trick.
Have Fun.
 
as root try this:

ln -s /opt/local/bin/perl /usr/bin/perl

This makes a (soft) link from your existing perl executable to file name where all of those example scripts expect it to be.

Mike
michael.j.lacey@ntlworld.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top