At 05:32 PM 4/24/02 -0400, Leon, Yanet I,,DMDCWEST wrote:
>I have been trying to replace a string:
>This-ismy-oldstring
>to
>This-ismy-newstring
>
>on a unix box
>
>underneath a directory, which contain files and subdirectories containing
>other files, which have my This-ismy-oldstring.  I need to make a global
>replacement substituting This-ismy-oldstring to This-ismy-newstring
>
>I have managed to do it opening a single file, but I need to do it in
>multiple files.  What is a good approach to accomplish this task?  I was
>thinking in doing a use:
>Shell qw (ls);
>$main_dir_contents=ls ( "-R");
>
>and somehow make the script to do a for loop, i.e for i in
>$main_dir_contents, open it and make the replacement.  Please, help... I am
>a beginner and I would like to get this accomplished.  Please, let me know
>your ideas.
>
>Part of my scripts looks as follows:
>open(FH, "+</etc/inetd.conf");
>while(<FH>) {
>      if($_=~s/^\#(shell\s.*\sin.rshd)$/$1/){
>          chomp($line=$_);
>          $currentpos=tell;
>      }
>}
>$currentpos=$currentpos-72;
>seek(FH,$currentpos,0);
>print FH " $line";

Ay caramba.  It looks as though the following (note: a one-liner, not even 
a script) should work for you:

perl -pi -e 's/^\#(shell\s.*\sin.rshd)$/$1/' *

If you want to save the original versions of the files (recommended in case 
one of us has made a mistake):

perl -pi.bak -e 's/^\#(shell\s.*\sin.rshd)$/$1/' *

If you want to do it on all files underneath the current directory:

find . -type f -print | xargs perl -pi.bak -e 's/^\#(shell\s.*\sin.rshd)$/$1/'

Well worth reading about -p and -i via "perldoc perlrun".



--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to