Search and Replace in Files? 
Author Message
 Search and Replace in Files?

Hi Group - my script is replacing in the first file it encounters, but
then it bails. Not sure why. WHen I'm printing my system command it is
looping through two files:


  print "changing $file\n";

Quote:
}

$what = $ARGV[1];
$newtext = $ARGV[3];

print "Would you like to proceed? (Y/N)";
$x = <STDIN>;
chop($x);

if ($x eq "Y") {


    $cc = "";
    $file2 = $file;
    open (test, "<$file");
    # $file =~s /^*.html*//;

    while ($ct = <test>) {
      chomp $ct;
      if ($ct =~ /$ARGV[1]/) {#need to replace that line with ww2
          system "perl -pi -e 's/$ARGV[1]/$ARGV[3]/g' $file";
          print "system = perl -pi -e 's/$ARGV[1]/$ARGV[3]/g'
$file\n";
      }
    $cc .= $ct."\n";
    }

  }

close (test);
open(NEW,">$file2");
print NEW $cc;
close(NEW);

Quote:
} else {

< snip >

Output:
# ./replacemany.cgi -w 1 -n david
changing david.html
changing lynda.html
Would you like to proceed? (Y/N)Y
system = perl -pi -e 's/1/david/g' david.html
system = perl -pi -e 's/1/david/g' david.html
system = perl -pi -e 's/1/david/g' lynda.html
system = perl -pi -e 's/1/david/g' lynda.html



Tue, 22 Nov 2005 19:08:15 GMT  
 Search and Replace in Files?


Quote:
> Hi Group - my script is replacing in the first file it encounters, but
> then it bails. Not sure why.

I'm not sure why either; your code is very difficult to follow and
unnecessarily roundabout.

1.  Did you call 'use strict;' at the top of this script.  Given that you
have no scoping of variables indicated, I suspect you didn't.
2.  Did you call 'use warnings;' or call the '-w' flag on the shebang line?
Again, I suspect not.

Quote:
> WHen I'm printing my system command it is
> looping through two files:


>   print "changing $file\n";
> }

> $what = $ARGV[1];
> $newtext = $ARGV[3];

You assign to $what and $newtext and never call them hereafter.

Quote:
> print "Would you like to proceed? (Y/N)";
> $x = <STDIN>;
> chop($x);

Poor style.  What you want is:   chomp($x = <STDIN>);

Quote:
> if ($x eq "Y") {


>     $cc = "";
>     $file2 = $file;
>     open (test, "<$file");
>     # $file =~s /^*.html*//;

>     while ($ct = <test>) {
>       chomp $ct;
>       if ($ct =~ /$ARGV[1]/) {#need to replace that line with ww2
>           system "perl -pi -e 's/$ARGV[1]/$ARGV[3]/g' $file";
>           print "system = perl -pi -e 's/$ARGV[1]/$ARGV[3]/g'
> $file\n";

Why do a system call ... and a system call to perl's own command-line
interpreter to boot ... when you can do it all inside Perl?

Quote:
>       }
>     $cc .= $ct."\n";
>     }

>   }

> close (test);
> open(NEW,">$file2");
> print NEW $cc;
> close(NEW);

I suspect that what you're trying to accomplish is to change particular
lines in a number of files.  These days the best way to do that is with the
'Tie::File' module by Mark Jason Dominus, available from CPAN and standard
with Perl 5.8.


Wed, 23 Nov 2005 00:28:39 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Search and Replace multiple files?

2. search and replace within files accross multiple lines ??

3. global search and replace in files

4. search and replace in a file tree, only for files that match

5. Search and replace keywords in a text file

6. how do i search and replace within a file

7. Searching and replacing strings in a file

8. search and replace text in many files

9. multi-file search/replace?

10. search/replace in multiple files.

11. many file search and replace (Windows)

 

 
Powered by phpBB® Forum Software