Substituting Strings in Many Files 
Author Message
 Substituting Strings in Many Files

Hello. How can I pass parameters into a Perl Script? Haven't done much
Perling, but Email replies would be greatly appreciated. Thank you.


Ex: Substituting strings in many files

Very useful script. Goes like this:

        perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' FILELIST...

as an example... Now, if I was using this example, I want to save this
file as "subst.pl". I would like to invoke this Perl script in the future
by calling it at the bash$ prompt:

        subst OLDSTRING NEWSTRING FILE1 FILE2 FILE3 FILEn...

Where in the list of files "FILE1...", all occurrences of the string
OLDSTRING will be replaced with NEWSTRING. Is this possible? Of course, but
could someone help me? Thanks.

        Bye,

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     Mukund Rao         !     http://www.*-*-*.com/ ~mrao
  Computer Science      !      Trinity College * BOX 700416
...............................................................
  Then there was the assertive toddler who called his favorite
  store "Toys R Else." --Readers Digest



Tue, 10 Nov 1998 03:00:00 GMT  
 Substituting Strings in Many Files

Quote:

> I would like to invoke this Perl script in the future
> by calling it at the bash$ prompt:
> =
>    subst OLDSTRING NEWSTRING FILE1 FILE2 FILE3 FILEn...
> =

Hello. Sure it can be done. You use the "while(<>)" construct
and take care of the arguments before you start looping over
the files.

#!/usr/bin/perl -i.bak =

#
$src_string =3D shift;     # Get the first two arguments off the command
=A3subst_string =3D shift;   # line

# Other fancy processing goes here, like letting the
# user decide wether or not to do case sensitive search etc.
# and some argument checking perhaps? =

# And then start looping over the files

while(<>) {
   s/$src_string/$subst_string/og;
   print;

Quote:
}

Hope this helps.

/Fredrik



Wed, 11 Nov 1998 03:00:00 GMT  
 Substituting Strings in Many Files

Quote:

> file as "subst.pl". I would like to invoke this Perl script in the future
> by calling it at the bash$ prompt:
>    subst OLDSTRING NEWSTRING FILE1 FILE2 FILE3 FILEn...

        Try calling it subst, and using chmod u+x subst

Quote:
>    perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' FILELIST...

#!/usr/bin/perl


        open(FILE, $file);
        foreach $line (<FILE>) {
                $line =~ s/$old/$new/g;
                print $line;
        }
        close(FILE);

Quote:
}

        That doesn't do everything, like split-line strings, and it
directs everything to STDOUT.  But to fix that, use this:-

#!/usr/bin/perl


        open(FILE, $file);
        open(TMP, "tmp");
        foreach $line (<FILE>) {
                $line =~ s/$old/$new/g;
                print TMP $line;
        }
        close(TMP);
        close(FILE);
        system("cp -i tmp $file");

Quote:
}

        Ok, so eventually you'll want to get rid of the -i, but until
you're 100% sure this is safe, you might want to leave it in.  And who
knows, you may like it :).  

--
---------------------------------------------------------------------
| Name: Tim Nelson          | Live and learn, die and forget --     |

---------------------------------------------------------------------
Web: http://www.deakin.edu.au/~tsn/

----BEGIN GEEK CODE BLOCK----
Version 3.1
GCS d? s: a--->-- C++>++++$ US+ P+ L>++ E- W+++ N+ w+> M-- V- Y+>++ PGP->++ R(+)
!tv B++ DI++++ D+ G e>++ h!/* y-
-----END GEEK CODE BLOCK-----



Wed, 18 Nov 1998 03:00:00 GMT  
 Substituting Strings in Many Files



Quote:
>>    subst OLDSTRING NEWSTRING FILE1 FILE2 FILE3 FILEn...

Try Programming Perl page 253.

        perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' FILELIST

--
David Richards



Thu, 19 Nov 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. substituting strings in many files

2. Substituting many sparsely distrbuted strings in many files

3. how to substitute strings in a newly created file

4. Substituting $variable strings in a file

5. Substituting strings (with newlines) in many files

6. split and substitute, substitute, substitute

7. Substituting many strings in one pass over the input

8. substituting strings in source trees?

9. Pattern Match - substitute a string after the match

10. Repeat string matching/substituting ?

11. substituting string of text

12. substitute a string in array

 

 
Powered by phpBB® Forum Software