Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help in scripting

Newbie
 
Join Date: Mar 2008
Posts: 1
#1: Mar 25 '08
I need to write a perl script to replace text in a series of files.
What i need to do:
1) in Command line i need to specify the <text to be replaced> , <replacing text>, <name of the files in which i need replacement>
2) Also i need to specify an option '-c' to prompt the user to confirm while replacing in each file.

That is the command line needs to look like this:

#<program name> -c <text to be replaced> <replacing text> <file1> <file2> etc...

could anyone please help me out on this. Am a beginner, so not sure of how the program would go...

nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#2: Mar 25 '08

re: Need help in scripting


What have you tried so far?
Member
 
Join Date: Mar 2008
Posts: 109
#3: Mar 25 '08

re: Need help in scripting


start with

#!/usr/bin/perl

and go from there :)
kalyanrajsista's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 7
#4: Mar 28 '08

re: Need help in scripting


I think this script will help you out...I've tested this on Unix and a homework for you to test on Windows.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. #
  3. # Replaces a string within multiple files
  4. # specified on the command line
  5.  
  6. $mv = '/bin/mv';
  7.  
  8. $op = shift || die("Usage: $0 perlexpr [filenames]\n");
  9.  
  10. if (!@ARGV) {
  11.   @ARGV = <STDIN>;
  12.   chop(@ARGV);
  13. }
  14.  
  15. foreach $file (@ARGV) {
  16.   if (!-f $file) {
  17.        print "Skipping non-regular file: $file\n";
  18.        next;
  19.   }
  20.   if (-B $file) {
  21.        print "Skipping binary file: $file\n";
  22.        next;
  23.   }
  24.  
  25.   $outfile = "/usr/tmp/$file.$$";
  26.  
  27.   open(FILE, $file) ||
  28.        die("Couldn't open $file: $!\n");
  29.   undef $/;
  30.   $_ = <FILE>;
  31.   close(FILE);
  32.  
  33.   if (eval $op) {
  34.     open(OFILE, "> $outfile") ||
  35.         die("Couldn't open $outfile: $!\n");
  36.     print OFILE;
  37.     close(OFILE);
  38.  
  39.     system($mv, '-f', $file, "$file.bak");
  40.     system($mv, '-f', $outfile, $file);
  41.  
  42.     print "File changed: $file\n";
  43.   }
  44.   else {
  45.     print "No change to file: $file\n";
  46.   }
  47. }
  48.  
  49. exit(0);
  50.  
Kalyan
kalyanrajsista's Avatar
Newbie
 
Join Date: Mar 2008
Posts: 7
#5: Mar 28 '08

re: Need help in scripting


Assuming that script name is replace

usage is replace 'substitute expression' list of files

Expand|Select|Wrap|Line Numbers
  1.  
  2. replace 's/this/that/g' 1.txt 2.txt 3.txt 4.txt
  3.  
  4.  
eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#6: Mar 28 '08

re: Need help in scripting


Welcome to TSDN Kalyan!

We like for the OP's to show effort in trying their task before handing them a solution. This will aid in helping them learn rather than doing it for them.

We appreciate your help.

--Kevin
Reply