Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

changing text in multiple files

by Anonymous Monk
on Jun 28, 2004 at 15:09 UTC ( [id://370214]=perlquestion: print w/replies, xml ) Need Help??

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

In a group of html files in a single directory I went to change all instances of 2003 to 2004 in each file.
How can I do this in PERL? (no this is not homework)
Thanks in advance for your wisdom!

Replies are listed 'Best First'.
Re: changing text in multiple files
by gellyfish (Monsignor) on Jun 28, 2004 at 15:12 UTC

    perl -pi -e's/2003/2004/g' *.html

    should do it.

    /J\

Re: changing text in multiple files
by bassplayer (Monsignor) on Jun 28, 2004 at 15:13 UTC
    perl -p -i.bak -e 's/2003/2004/g' *.html

    This will edit all files in place, after making a backup copy called <filename>.bak.

    bassplayer

Re: changing text in multiple files
by Limbic~Region (Chancellor) on Jun 28, 2004 at 15:26 UTC
    Anonymous Monk,
    While you have several replies that address changing 2003 to 2004 universally in a bunch of files, no one has mentioned that "rendered" text often does not look like the underlying HTML. Many tags could be "imbedded" in the text that you do not see as an end product. The methods provided so far do not address this. You will need to use one of the numerous HTML parsers such as HTML::TokeParser::Simple available on CPAN to get those cases. The problem is keeping track of the tags so the formatting is not lost either.

    Cheers - L~R

Re: changing text in multiple files
by blue_cowdawg (Monsignor) on Jun 28, 2004 at 15:21 UTC

        n a group of html files in a single directory I went to change all instances of 2003 to 2004 in each file.

    There are many ways of skinning that cat and the only one who won't be happy will be the cat.

    One way:

    #!/usr/bin/perl -w ############################################# # useage: update.pl PATH # where PATH is the full path of the directory # we want to fix the files in. use strict; use Tie::File; exit(0) unless $ARGV[0]; chdir $ARGV[0] or die "Cound not chdir to " . $ARGV[0]; # # Create your own messages for above. my @files=glob("*.html"); foreach my $file(@files){ my @ry=(); tie @ry,"Tie::File",$file or die "Could not open $file"; $_=~s/2003/2004/g foreach @ry; utie @ry; }
    Just one of many, many, ways of doing this.

Re: changing text in multiple files
by Plankton (Vicar) on Jun 28, 2004 at 15:17 UTC
    $ perl -pi -e 's/2003/2004/g' *

    Plankton: 1% Evil, 99% Hot Gas.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 00:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found