Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris


by gok8000 (Scribe)
on Dec 02, 2008 at 15:47 UTC ( #727451=snippet: print w/ replies, xml ) Need Help??

Hi all,

Sometimes I have to change words inside text files. When this happens, and when I know what to search for, I use this program.

# changes words inside text files
# placed in the win directory C:\filestochange
# which is supposed to contain text files

my $dir_to_process = "C:\\filestochange";
opendir DH, $dir_to_process or die "Cannot open $dir_to_process: $!";

foreach $file (readdir DH) {
  unless ($file eq "." || $file eq ".." || $file eq "discarded" || $fi
+le eq "tmp") {
    print "in $dir_to_process $file is processed\n";
    open (INFILEHANDLE, "C:\\filestochange\\$file") or die "error open
    open (OUTFILEHANDLE, ">C:\\filestochange\\tempfile8000") or die "e
+rror opening";
    while (<INFILEHANDLE>) {
      # chomp;
      s/this/that/; # substututes this with that
      s/one/two/; # substututes one with two
      print OUTFILEHANDLE;
    rename "C:\\filestochange\\tempfile8000","C:\\filestochange\\$file

closedir DH;

print "\nDone (press enter key)\n";
$line = <STDIN>;
Replies are listed 'Best First'.
Re: Corrector
by hossman (Prior) on Dec 02, 2008 at 16:57 UTC

    May i humbly suggest an alternative...

    #!/usr/bin/perl -pi s/this/that/; # substitutes this with that s/one/two/; # substitutes one with two

    Run with: C:\filestochange

    It doesn't have some of the interesting side effects of the original script, but i consider that a good thing.

    Updated: there is no "i" in team, but there should have been one on the first line
      #!/usr/bin/perl -p s/this/that/; # substitutes this with that s/one/two/; # substitutes one with two

      It also doesn't work. Did you even try it?

        Nope, didn't give it any thought at all. should be fixed now.

Re: Corrector
by quinkan (Monk) on Dec 13, 2008 at 06:02 UTC
    Save yourself effort and use one of the "Slurp" modules, given that Perl 6 is to support even smarter things. These let you read all file content into a single variable. Less code to go wrong. Thus, one fragment might read:
    use File::Slurp: # get one file here, as $file my($bakfile, $file, $text); # maybe set up file renaming with something like this... ($bakfile = $file ) =~ s/\.([^.]+)$/\.bak/; # then, the main bit $text = read_file($file); $text =~ s/this/that/smg; rename($file, $bakfile); write_file($file, $text);
    Another possible version something like:
    use Perl6::Slurp; #<= only handles input at present use File::Slurp; # permits writing. # and then.... .... $text = slurp($file); $text =~ s/$this/$that/smg; ... with renaming here ? (see above). write_file($file, $text);
    And BTW do you really need that chomp and/or intend to remove newlines?

      Thank you for the suggestion.

      The commented chomp was left in the source for a possible future use, but at present newlines are not removed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://727451]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2016-07-26 09:59 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (234 votes). Check out past polls.