Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Parsing each file of a directory to change once string instance to another

by z28 (Sexton)
on Mar 20, 2002 at 20:35 UTC ( [id://153120]=perlquestion: print w/replies, xml ) Need Help??

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; } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://153120]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-12-08 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.