in reply to Need to replace string from external file

Since the task itself isn't that complex, can you show us what code you have already written?

Please also show a short example of input data, the expected output and the output you get instead.

This way, we can give you good advice that actually adresses the code you have.

  • Comment on Re: Need to replace string from external file

Replies are listed 'Best First'.
Re^2: Need to replace string from external file
by bhupi70007 (Novice) on Nov 06, 2017 at 11:46 UTC
    HI Corion Please find my code as follow
    use strict; use warnings; use Path::Tiny qw(path); my $filenme='d:\SampleCiscoIOSEvent.txt'; my $file=path($filenme); my $data= $file->slurp_utf8; use File::Slurp; my $filename='e:\filename.txt'; my @arr = read_file($filename); my @output= map{ $data=~s/C_USERNAME/$_/rg} @arr; print @output;
Re^2: Need to replace string from external file
by bhupi70007 (Novice) on Nov 06, 2017 at 11:40 UTC

    HI corion

    I have written this code

    use strict; use warnings; use Path::Tiny qw(path); my $filenme='d:\SampleCiscoIOSEvent.txt'; my $file=path($filenme); my $data= $file->slurp_utf8; use File::Slurp; my $filename='e:\filename.txt'; my @arr = read_file($filename); my @output= map{ $data=~s/C_USERNAME/$_/rg} @arr; print @output;

    the problem is it is working fine for one variable But if i have more then 1 variable my output is very confusing Please help me

    My code for changing more then one variable is as follow

    use strict; use warnings; use Path::Tiny qw(path); my $filenme='d:\SampleCicoIOSEvent.txt'; my $file=path($filenme); my $data= $file->slurp_utf8; use File::Slurp; my $filename='e:\filename.txt'; my $IPfilename='e:\IPfilename.txt'; my @arr = read_file($filename); my @arr1 = read_file($IPfilename); my @output= map{ $data=~s/C_USERNAME/$_/rg} @arr; my @output1= map{ $data=~s/IP/$_/rg} @arr1; print @output; print @output1;
      my @output= map{ $data=~s/C_USERNAME/$_/rg} @arr; my @output1= map{ $data=~s/IP/$_/rg} @arr1;

      This is almost correct, but note that you are storing the first transformation, using C_USERNAME in @output, and then the second transformation, using IP in @output1. Most likely, you will want to transform everything, after having replaced all C_USERNAME and all IP.

      Instead of reusing $data in the second output, you will likely want to use the elements in @output.

      my @usernames = map { $data =~ s/C_USERNAME/$_/rg } @arr; my @user_and_ip; for my $user_string (@usernames) { push @user_and_ip, map { $user_string =~ s/IP/$_/rg } @arr1; }

      A better approach would likely be to use a templating engine over your data.

        Hey corion Thanks a ton.Your code somewhat help me .
        brother i cannot see use of external file in your program for storing the replacment value.. I am just a beginner Need spoon feeding. You help will be highly appreciated. I have created this program but just doing R& D on google .
        A reply falls below the community's threshold of quality. You may see it by logging in.
      This is the second program that i created for replacing two argument. But it is getting complex
      use strict; use warnings; use Path::Tiny qw(path); my $filenme='d:\SampleCicoIOSEvent.txt'; my $file=path($filenme); my $data= $file->slurp_utf8; use File::Slurp; my $filename='e:\filename.txt'; my $IPfilename='e:\IPfilename.txt'; my @arr = read_file($filename); my @arr1 = read_file($IPfilename); my @output= map{ $data=~s/C_USERNAME/$_/rg} @arr; my @output1= map{ $data=~s/IP/$_/rg} @arr1; print @output; print @output1;