Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    25

    Unanswered: search and replace multiple string

    I want to know for any occurrence of strings in my data file.csv how can I replaced them with new value as
    Example
    $infile = 'C:\tmp\test.csv';
    $outfile = 'C:\tmp\output.csv;
    $orgtext_1 = 'data file (perl)';
    $newtext_1 = 'dfp';
    $orgtext_2= 'Logged in user(perluser)'
    $newtext_2= 'liu'

    open IN, $infile or die $!;
    $intext = <IN>;
    ......
    ......
    ......
    If my search string contain () as above I do not know how to search then
    Last edited by lonaj; 03-03-04 at 22:44.

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    I think you would like to replace all orgtexts with the newtexts. Am I right??

    To replace anything you have to use the s/// operator...

    Then you should do it like this:
    Code:
    #! /usr/bin/perl
    
    use strict; # to avoid misspellings, declare all variables with 'my'
    use warnings; # to get warnings
    use diagnostics;
    
    my $infile = 'C:\tmp\test.csv';
    my $outfile = 'C:\tmp\output.csv';
    my $orgtext_1 = 'data file (perl)';
    my $newtext_1 = 'dfp';
    my $orgtext_2= 'Logged in user(perluser)';
    my $newtext_2= 'liu';
    
    open IN, "<$infile" or die $!;
    open OUT, ">$outfile" or die $!;
    while(my $intext = <IN>){
      $intext =~ s/$orgtext_1/$newtext_1/g; # replace all (therefore the flag g is set) $orgtext_1 with $newtext_1
      $intext =~ s/$orgtext_2/$newtext_2/g;
      print OUT $infile;
    }
    close OUT;
    close IN;
    Last edited by reneeb; 03-09-04 at 02:27.
    board.perl-community.de - The German Perl-Community

  3. #3
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    I fear something rather subtle occurs with Reneeb's solution:

    Quote Originally Posted by reneeb
    I think you would like to replace all orgtexts with the newtexts. Am I right??

    To replace anything you have to use the s/// operator...

    Then you should do it like this:
    Code:
    #! /usr/bin/perl
    
    use strict; # to avoid misspellings, declare all variables with 'my'
    use warnings; # to get warnings
    use diagnostics;
    
    my $infile = 'C:\tmp\test.csv';
    my $outfile = 'C:\tmp\output.csv';
    my $orgtext_1 = 'data file (perl)';
    my $newtext_1 = 'dfp';
    my $orgtext_2= 'Logged in user(perluser)';
    my $newtext_2= 'liu';
    
    open IN, "<$infile" or die $!;
    open OUT, ">$outfile" or die $!;
    while(my $intext = <IN>){
      $intext =~ s/$orgtext_1/$newtext_1/g; # replace all (therefore the flag g is set) $orgtext_1 with $newtext_1
      $intext =~ s/$orgtext_2/$newtext_2/g;
      print OUT $infile;
    }
    close OUT;
    close IN;
    "(" and ")" have special meaning with a regex and hence they will need to be escaped. Rather than 'data file (perl)', one needs to use 'data file \(perl\)'. So we can try ... CAVEAT: don't do this on huge files unless you have lots of free memory.

    Code:
    #! /usr/bin/perl -w
    
    use strict;
    use warnings;
    use diagnostics;
    
    my $infile = 'C:test.csv';
    my $outfile = 'C:output.csv';
    my(%h)=('data file \(perl\)'=>'dfp',
            'Logged in user\(perluser\)'=>'liu');
    
    undef($/);
    open IN, "<$infile" or die $!;
    open OUT, ">$outfile" or die $!;
    $_=<IN>;
    close IN;
    
    foreach my $key (keys %h) {
      s/$key/$h{$key}/g;
       };
    
    print OUT $_;
    close OUT;

  4. #4
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    167
    Ok, You're right. You have to escape the (), but I've use the while-loop to save memory. If you use the while-loop, the space which was allocated by the $intext, is available for the next line. So you can minimize your memory used.
    board.perl-community.de - The German Perl-Community

  5. #5
    Join Date
    Jun 2004
    Location
    Nowhere Near You
    Posts
    89
    Which brings us to (note the use of map {quotemeta })
    Code:
    #! /usr/bin/perl -w
    
    use strict;
    use warnings;
    use diagnostics;
    
    my $infile = 'C:test.csv';
    my $outfile = 'C:output.csv';
    my(%h)=map {quotemeta} ('data file (perl)'=>'dfp',
            'Logged in user(perluser)'=>'liu');
    
    undef($/);
    open IN, "<$infile" or die $!;
    open OUT, ">$outfile" or die $!;
    $_=<IN>;
    close IN;
    
    foreach my $key (keys %h) {
      s/$key/$h{$key}/g;
       };
    
    print OUT $_;
    close OUT;
    Reneeb is quite correct, the above code trades memory for speed!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •