Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

changing inside a file

by amoura (Initiate)
on Aug 26, 2002 at 16:38 UTC ( [id://192934]=perlquestion: print w/replies, xml ) Need Help??

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

Hi ,, I have a tree of files I am trying to go inside each one and change one word from old to new somthing like :
while files { open (firstFile, " c:/file")or die; if ( $_ eq " old " ) $_ = " new "; }
so I got the logic but how do I open the directory containg these files and its subfiles and start reading each file at the time knowing that some files have subfiles which need to be changed as well ,, thanks

Replies are listed 'Best First'.
Re: changing inside a file
by waswas-fng (Curate) on Aug 26, 2002 at 17:01 UTC
    find . -name "*.html" -exec perl -pi -e's/ old / new /' {} \;;

    -Waswas
      this seems to work but I get this error
      Can't do inplace edit without backup.
        find . -name "*.html" -exec perl -pi.bak -e's/ old / new /' {} \; ; This will put a <filename>.bak backup file in place for every file that gets updated.. you must be using Activestate perl huh? mine allows inplace without a backup..

        -Waswas
Re: changing inside a file
by derby (Abbot) on Aug 26, 2002 at 16:47 UTC
    checking out File::Find would be a good start. That way you don't have to worry about recursing the directory structure yourself.

    -derby

Re: changing inside a file
by bart (Canon) on Aug 26, 2002 at 18:58 UTC
    Somehow I have the feeling that the standard solutions, like te ones proposed here, don't really apply to your problem. I guess that you're actually trying to process in include-like tree, where some lines in a file point to other files, which you want to include as well... Am I right?

    In that case, I would think somewhat in this direction:

    my @todo = glob('c:/*.inc'); # some kind of file my %done; undef $/; while(@todo) { my $file = shift @todo; open FILE, "+<$file" or die "Can't open file $file: $!"; $_ = <FILE>; # whole contents of file if(s/\bold\b/new/g) { # replace contents of file seek FILE, 0, 0; truncate FILE, 0; print FILE; } my($dir) = $file =~ m[(.*/)]; while(/^#include (\S+);/mg) { push @todo, "$dir$1" unless $done{"$dir$1"}++; } }
    But I must admit that I haven't actually put this to a test, I only made sure it compiles. It's just to give you some ideas.
      ;) nice ,, thanks
Re: changing inside a file
by screamingeagle (Curate) on Aug 26, 2002 at 16:54 UTC
    File::Recurse might be another option...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-25 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found