By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,844 Members | 1,413 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,844 IT Pros & Developers. It's quick & easy.

Need help in scripting

P: 1
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...
Mar 25 '08 #1
Share this Question
Share on Google+
5 Replies


nithinpes
Expert 100+
P: 410
What have you tried so far?
Mar 25 '08 #2

100+
P: 109
start with

#!/usr/bin/perl

and go from there :)
Mar 25 '08 #3

kalyanrajsista
P: 7
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
Mar 28 '08 #4

kalyanrajsista
P: 7
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.  
Mar 28 '08 #5

eWish
Expert 100+
P: 971
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
Mar 28 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.