z28 has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I need you again in this hour, when this change is so critical, and my lunch break so near...


I loop through /home/onyx2/www/ and get all files with an .shtml extension. This works.

I open every file, one at a time. This works.

I run the regexp to change out foo-fuu.net with fuuonline.com and it does not work.

What gives? Code below:


#!/usr/bin/perl use strict; my @list = glob("/home/onyx2/www/*.shtml"); foreach my $file (@list) { open FILE, "$file" or die "open: $!"; for (<FILE>) { $_ =~ s%foo\-fuu\.net%fuuonline\.com%ig; print FILE $_; } close FILE; }
  • Comment on Parsing each file of a directory to change once string instance to another
  • Download Code

Replies are listed 'Best First'.
Re: Parsing each file of a directory to change once string instance to another
by rah (Monk) on Mar 21, 2002 at 03:56 UTC
    How about a one-liner?
    perl -p -i -e 's %foo\-fuu\.net%fuuonline\.com%g' /home/onyx2/www/*.sh +tml
Re: Parsing each file of a directory to change once string instance to another
by metadoktor (Hermit) on Mar 20, 2002 at 20:49 UTC
    You opened the file for reading but you're trying to write to it too!

    You need to write your data out to another file.

    metadoktor

    "The doktor is in."

      Ah! I want to write to the same file that I just opened. Is this possible?
Re: Parsing each file of a directory to change once string instance to another
by Anonymous Monk on Mar 20, 2002 at 23:13 UTC
    {local($^I,@ARGV)=('.bak',@list); while( <> ){ s%foo\-fuu\.net%fuuonline\.com%ig; print; } }