FAQ
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.

Search Discussions

  • Paul Lalli at Aug 9, 2007 at 6:26 pm

    On Aug 9, 11:12 am, kev...@gmail.com wrote:
    I am trying to replace a string within a bunch of html files.
    .... so why does your subject say CSV?
    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?
    This is fairly trivial. What part of it is giving you a problem?

    [untested]
    #!/usr/bin/perl
    use strict;
    use warnings;

    open my $rep_fh, '<', 'replacement.txt' or die $!;
    chomp (my $repl = <$rep_fh>);
    close $rep_fh;

    open my $file_fh, '<', "file_list.txt" or die $!;
    while (my $filename = <$file_fh>) {
    chomp $filename;
    open my $fh, '<', $filename or die $!;
    open my $ofh, '>', "$filename.mod" or die $!;
    while (<$fh>) {
    s/string_to_replace/$repl/g;
    print $ofh $_;
    }
    close $fh;
    close $ofh;
    }
    close $file_fh;
    __END__


    Paul Lalli
  • Jeff Pang at Aug 10, 2007 at 12:57 am

    -----Original Message-----
    From: kevc73@gmail.com
    Sent: Aug 9, 2007 11:12 PM
    To: beginners@perl.org
    Subject: Find and replace from CSV

    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?
    Make one for you using both shell and perl,:)

    #!/bin/sh
    FILE=$1
    REPLACED=$2
    REPLACEMENT=$3

    perl -pi.bak -e 's/\Q$REPLACED\E/$REPLACEMENT/' $FILE
    echo "DONE"

    ------
    HTH.

    --
    Jeff Pang <pangj@earthlink.net>
    http://home.arcor.de/jeffpang/
  • Jeff Pang at Aug 10, 2007 at 5:24 am

    -----Original Message-----
    From: Jeff Pang <pangj@earthlink.net>
    #!/bin/sh
    FILE=$1
    REPLACED=$2
    REPLACEMENT=$3

    perl -pi.bak -e 's/\Q$REPLACED\E/$REPLACEMENT/' $FILE
    echo "DONE"
    Found the problem,here shell variables can't be passed into Perl program.
    Maybe using environment variables,or better solution?

    #!/bin/sh
    FILE=$1
    export REPLACED=$2
    export REPLACEMENT=$3

    perl -pi.bak -e 's/\Q$ENV{REPLACED}\E/$ENV{REPLACEMENT}/' $FILE
    echo "DONE"

    --
    Jeff Pang <pangj@earthlink.net>
    http://home.arcor.de/jeffpang/
  • Purohit, Bhargav at Aug 10, 2007 at 11:14 am
    -----Original Message-----
    From: Jeff Pang
    Sent: Friday, August 10, 2007 10:54 AM
    To: beginners@perl.org
    Subject: Re: Find and replace from CSV


    -----Original Message-----
    From: Jeff Pang <pangj@earthlink.net>
    #!/bin/sh
    FILE=$1
    REPLACED=$2
    REPLACEMENT=$3

    perl -pi.bak -e 's/\Q$REPLACED\E/$REPLACEMENT/' $FILE echo "DONE"
    Found the problem,here shell variables can't be passed into Perl
    program.
    Maybe using environment variables,or better solution?

    #!/bin/sh
    FILE=$1
    export REPLACED=$2
    export REPLACEMENT=$3

    perl -pi.bak -e 's/\Q$ENV{REPLACED}\E/$ENV{REPLACEMENT}/' $FILE
    echo "DONE"

    --
    Jeff Pang <pangj@earthlink.net>
    http://home.arcor.de/jeffpang/


    This is also not working

    I have also tried

    X="Hi"
    Export $X
    Perl -e 'print $ENV{x}'

    But it also do not works !

    Can u ppl give some other idea as I too want to call perl script from
    within the shell progs.

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice.
  • Chas Owens at Aug 10, 2007 at 4:06 pm
    On 8/10/07, Purohit, Bhargav wrote:
    snip
    X="Hi"
    Export $X
    Perl -e 'print $ENV{x}'

    But it also do not works !
    snip

    That is because "Export $X" is not shell code (or at least not any
    shell I am familiar with). Even "export $X", which is valid shell
    code, won't do what you want since it means "export Hi". What you
    want is

    cowens@amans:~$ X="Hi"
    cowens@amans:~$ export X
    cowens@amans:~$ perl -le 'print $ENV{X}'
    Hi
    cowens@amans:~$
  • Kevc73 at Aug 13, 2007 at 5:31 pm
    Thanks for all of the help.

    To answer Paul's first question, I think I didn't explain very well
    what I am trying to do.

    There are a few thousand html files that I need to modify. There is a
    string that I need to replace that will be different in each file.
    example: author_id = '60000" (the 60k will be one of 7 different
    numbers. The replacement number will also be 5 digits, but each html
    file will be a different number.

    I have in a CSV file that contains the filenames and the corresponding
    5 digit replacement number. example: htmlfile_1.html,55634

    I need the script to open each html file listed in the CSV and replace
    the author_id number with the one in the CSV file.
  • Kevc73 at Aug 13, 2007 at 5:40 pm

    On Aug 9, 8:12 am, kev...@gmail.com 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.
  • Zhao, Bingfeng at Aug 14, 2007 at 4:37 am
    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+)/)
    {
    # 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;
    }
    }

    Don't tested. Hopes help

    -----Original Message-----
    From: kevc73@gmail.com
    Sent: Tuesday, August 14, 2007 1:39 AM
    To: beginners@perl.org
    Cc: kevc73@gmail.com
    Subject: Re: Find and replace from CSV
    On Aug 9, 8:12 am, kev...@gmail.com 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.










    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/


  • Zhao, Bingfeng at Aug 14, 2007 at 5:11 am
    Oops, The 23rd line should be
    rename $temp, $file;
    -----Original Message-----
    From: Zhao, Bingfeng
    Sent: Tuesday, August 14, 2007 12:37 PM
    To: kevc73@gmail.com; beginners@perl.org
    Subject: RE: Find and replace from CSV

    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+)/)
    {
    # 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;
    }
    }

    Don't tested. Hopes help

    -----Original Message-----
    From: kevc73@gmail.com
    Sent: Tuesday, August 14, 2007 1:39 AM
    To: beginners@perl.org
    Cc: kevc73@gmail.com
    Subject: Re: Find and replace from CSV
    On Aug 9, 8:12 am, kev...@gmail.com 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.










    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/


    --
    To unsubscribe, e-mail: beginners-unsubscribe@perl.org
    For additional commands, e-mail: beginners-help@perl.org
    http://learn.perl.org/


  • Mug at Aug 14, 2007 at 5:17 am

    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: kevc73@gmail.com
    Sent: Tuesday, August 14, 2007 1:39 AM
    To: beginners@perl.org
    Cc: kevc73@gmail.com
    Subject: Re: Find and replace from CSV
    On Aug 9, 8:12 am, kev...@gmail.com 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.
  • Kevc73 at Aug 16, 2007 at 9:32 pm
    Update:

    Thanks everyone for the help.

    I ended up using Mug's version.


    **************************************************************
    #/usr/bin/perl
    use strict;
    use warnings;

    my $csv = 'auth.txt'; # replace your csv file name here
    open FH, $csv or die "canot open file $csv: $!\n";
    for(<FH>)
    {
    chomp;
    if (/(.+?),(\d+)/)
    {
    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"
    }
    }

    }

    **************************************************************************

    You will notice I changed it from a CSV to a txt doc. Perl was adding
    " " around the filenames causing errors. Once it was reading the list
    from a txt file it worked great.

    Thanks again.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedAug 9, '07 at 3:13p
activeAug 16, '07 at 9:32p
posts12
users7
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase