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

I am trying to have perl read in a file, and search for a certain line in the program (name="name.com"). I want to replace the "name.com" with a variable that is in a file listing email addresses. This will occur several places within the file. I then want to save the file where the filename will be the same as the replacement variable. I have a list of 100 names, so I should end up with 100 files. Thanks for your help to a newbie!

Replies are listed 'Best First'.
(shockme) Re: Search and replace
by shockme (Chaplain) on Dec 02, 2001 at 04:01 UTC
    There are cleaner and shorter ways to code this, but since (1) you say you're new to Perl, (2) I don't want to confuse any obvious issues and (3) I think the following makes the solution more obvious, I'm coding *everything* in a straightforward manner.

    Pay particular attention the comments. You haven't made your particular situtation (file formats, etc.) completely known, so I've made some assumptions.

    At the very least, this should provide a good starting point. Good luck!

    #!/usr/bin/perl -w # # note: this assumes that each line in email_list.txt contains only # one email address use strict; open (EMAIL, "< email_list.txt") || die "cannot open email_list.txt: $ +!"; #open email address file while (my $email_address = <EMAIL>) { chomp($email_address); open (OUT, ">$email_address.txt") || die "cannot open $email_addre +ss: $!"; open (JOHN, "< john.txt") || die "cannot open john.txt: $!"; while (my $main_input = <JOHN>) { chomp($main_input); if ($main_input =~ /email=/) { # matching email addresses is tricky, so YMMV on the # + following $main_input =~ s/([a-zA-Z0-9]{3})\@([a-zA-Z0-9\.\-]*)\ +%?.*$/$email_address/; } print OUT "$main_input\n"; } close JOHN; close OUT; } close EMAIL;

    Update: Added close EMAIL;

    If things get any worse, I'll have to ask you to stop helping me.

Re: Search and replace
by broquaint (Abbot) on Dec 02, 2001 at 06:22 UTC
    I think this might be something along the lines of what you want ...
    use strict; use File::Copy; my $dest_file = "email.lst"; my $searchstr = 'blue'; my $repstr = 'foo'; open(FL, $dest_file) or die("Doh - $!"); # turn on inplace edit flag (see. man perlvar) $^I = '~'; s/$searchstr/$repstr/g while <FL>; close(FL); move($dest_file, $repstr);
    This just opens a file, does a search and replace within the file (see. the $^I variable), then renames it to the new file. This could probably be done in a line or two of shell and perl, but this is nice'n'pure :o)
    HTH

    broquaint

Re: Search and replace
by jlongino (Parson) on Dec 01, 2001 at 23:31 UTC
    You might want to elaborate on the replacement requirements. Are you trying to replace "name.come" with a complete email address? Does the file with email addresses contain one email address and no other text? Examples would help.

    Also it might be easier to help if you show us the code you have so far.

    --Jim

      I am having a hard time getting this started.

      To explain further what I want to do, I currently have 1 file with a list of email addresses. These I am trying to use as my variables. I have another text file where I need to search for "email=john.edu" and replace with email=new_address.edu. Then I would like to save this file with the changes as new_address.txt. I have 100 email addresses in my list. I would like this process to repeat until the end of the adresses. I should have 100 new text files in this directory.

      I am new to perl and really do not know where to start. I don't know how to read in my email list and search to replace just the new email address and save the entire file before continuing with the loop.

      open (FILEHANDLE, "<$john.txt"); #open main file open (DATA, "<email_list.txt"); @lines = <DATA>; $/ = "\"; while (<DATA>) { #PROCESS AND UPDATE A RECORD }
      If you can think of a better way to do this, please let me know. Thanks!!!!