substituting strings in many files 
Author Message
 substituting strings in many files

This procedure replaces all occurrences of 1 string with another in all
the arguments provided on the command line:

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

I am trying to write a script "substring.pl" that will do this, but can
be reused. Substring.pl should accept at least 4 arguments:

$OLDSTRING, $NEWSTRING, FILELIST (at least 2 files)

Questions:
1) How can I specify "display USAGE unless at least 4
arguments are given?
2) How can I run the substitution 's///g' counting from the third
argument onwards? I'd like the user to be able to call the script with a
fileLIST.

-------------------------------------------------------------------

Thanks in advance to all who reply. Could you please reply to this message

-------------------------------------------------------------------

The Script I have as of now:-

#!/usr/local/bin/perl



        print "*** Substituting string(s) in ", $#ARGV - 1, " files
***\n\n";
        }

    $OLDSTRING = $ARGV[0];
    $NEWSTRING = $ARGV[1];

    {
       $filename = $ARGV[$i];
       s/$OLDSTRING/$NEWSTRING/g

# I need help here ---^

    }

sub usage
{
        print "\nUSAGE: This script will replace all occurrences of 1 string with
another in all the files given on the command line...\n";
        print "EXAMPLE: substring OLDSTRING NEWSTRING FileList... \n\n";

Quote:
}

-------------------------------------------------------------------

        Bye,

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     Mukund Rao         !     http://www.*-*-*.com/ ~mrao
  Computer Science      !      Trinity College * BOX 700416
...............................................................
 Receptionist to physician: "The patient is in the middle of a
 magazine article and will see you soon, Doctor." --Readers Digest



Sun, 22 Nov 1998 03:00:00 GMT  
 substituting strings in many files

Follow-up... The FileList can actually contain _one_ or more filenames; I
mistakenly specified "at least two..." in my request.

Thanks in advance to all who reply. Could you please reply to this message

        Bye,

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     Mukund Rao         !    http://shakti.trincoll.edu/~mrao
  Computer Science      !      Trinity College * BOX 700416
...............................................................
 Man to co-worker: "Of course it's not your day, Ernie! With five
 billion people on earth, do you realize what the chances are
 against it being 'your' day?" --Readers Digest



Sun, 22 Nov 1998 03:00:00 GMT  
 substituting strings in many files

[mailed and posted]



This is untested, but hopefully it'll give you the general idea.

Quote:
>Questions:
>1) How can I specify "display USAGE unless at least 4
>arguments are given?

#/usr/local/bin/perl -pi.bak
# Check this in the Camel, but you'll want to give the 'edit in
# place' swtich in the first line of your script.

# you said at least 4 were required
&usage unless $ARGV[3];

Quote:

>    print "*** Substituting string(s) in ", $#ARGV - 1, " files
>***\n\n";

>    $OLDSTRING = $ARGV[0];

# use shift to get the values out of ARGV, leaving only filenames
# left in the array for processing.
$OLDSTRING = shift;

Quote:
>    $NEWSTRING = $ARGV[1];

# same thing here.
$NEWSTRING = shift;


print "Substiting $NEWSTRING for $OLDSTRING in $file\n";
        s/$OLDSTRING/$NEWSTRING/g;

Quote:
}

>    }

>sub usage
>{
>    print "\nUSAGE: This script will replace all occurrences of 1 string with
>another in all the files given on the command line...\n";
>    print "EXAMPLE: substring OLDSTRING NEWSTRING FileList... \n\n";
>}

>-------------------------------------------------------------------

>        Bye,

> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     Mukund Rao         !    http://shakti.trincoll.edu/~mrao
>  Computer Science      !      Trinity College * BOX 700416
>...............................................................
> Receptionist to physician: "The patient is in the middle of a
> magazine article and will see you soon, Doctor." --Readers Digest

--
Eric D. Friedman



Sun, 22 Nov 1998 03:00:00 GMT  
 substituting strings in many files

On Wed, 5 Jun 1996, Mukund Rao wrote something resembling:

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

> I am trying to write a script "substring.pl" that will do this, but can
> be reused. Substring.pl should accept at least 3 arguments:

> $OLDSTRING, $NEWSTRING, FILELIST
> 1) How can I specify "display USAGE unless at least 3
> arguments are given?


Quote:
> 2) How can I run the substitution 's///g' counting from the third
> argument onwards?



The rest is left as an exercise. :-)  Hint: See the perlrun(1) man page,
among others. Hope this helps!

-- Tom Phoenix            http://www.teleport.com/~rootbeer/




Sun, 22 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