Zhao, Bingfeng wrote:
use strict;
use warnings;

my $csv = your_csv_file; # replace your csv file name here
open FH, $csv or die "canot open file $csv: $!\n";
for(<FH>)
{
        chomp;
        if (/(.+?),(\d+)/)
I like this more :
my ( $file, $num ) = split /,/ , $_ ;

if ( -e $file && $num =~ /^\d{5}$/ ) {
  open HTML, $file or die $!;
   local $/ = undef;
   my $newContext = <HTML>;
   close HTML;
   $newContext =~ s/(author_id = )"(\d+)"/$1"$num"/g ;

   open HTML, ">$file" or die $!;
      print HTML $newContext;
   close HTML

else {
   print "$file - $num Error\n"
}
}

        {
                # we got file name in $1 and the number in $2
                my $file = $1
                my $num = $2;
                my $temp = ".temp";
                open FHTML, $file or warn "cannot open $1; $!, ignore\n"
and next;
                open TEM, ">$temp" or warn "cannot open temp file to
write\n" and next;
                for (<FHTML>)
                {
                        s/$1/$num/g if /author_id = "(\d+)"/;
                        print TEMP;
                }
                close TEMP;
                rename $file, $temp;
        }
}

I quite afraid the $1, $2 ... will easily mixed up, on the scopes.

HTH too, code not tested too =)

Don't tested. Hopes help
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Tuesday, August 14, 2007 1:39 AM
To: beginners@perl.org
Cc: [EMAIL PROTECTED]
Subject: Re: Find and replace from CSV

On Aug 9, 8:12 am, [EMAIL PROTECTED] wrote:
Hi, I am new to Perl.

I am trying to replace a string within a bunch of html files.

Ideally, I would like to have the file name pulled from a list in a
text file, open the file, search for the string that will
be replaced
within the file, pull the replacement string from another file, then
replace the string, close the file and move on to the next one.

Does anyone know of a good place to find an existing script I can
modify for my purposes?

Thanks.
Thanks for all of the replies.

To answer Paul's question, I think I wasn't very clear with what I am
trying to do.

There are a few thousand html files. Within each file I want to change
the numbers in this string: author_id = "60000";    (The 60k will
vary.) with numbers that will be file specific.

I have a csv file with the html file names and the corresponding
replacement numbers. example: html_file_1.html,66548
html_file_2.html,98754   html_file_3.html,24514 etc...

I need the script to open the files listed in the CSV and replace the
5 digit number in the html file with the one in the CSV.



Mug

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to