in reply to Re: Need to replace string from external file
in thread Need to replace string from external file

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;

Replies are listed 'Best First'.
Re^3: Need to replace string from external file
by marto (Cardinal) on Nov 06, 2017 at 11:49 UTC

    Are you copying and pasting this from somewhere? Why are you using File::Slurp when you're already using Path::Tiny?

      yup i am copying as i have already created a script for it.

        I mean did you find the basis for this online somewhere, which you'd copied/pasted. Don't use File::Slurp (explained here, and elsewhere) as you're already using File::Path.

Re^3: Need to replace string from external file
by 1nickt (Canon) on Nov 06, 2017 at 12:10 UTC

    Hi, that's helpful, thanks. Please see my post and the links in it, as well as any other well-formed question, for examples of how to post code + input + output.

    It's still not possible to see what the contents of the files are, and what you wish the output to be.


    The way forward always starts with a minimal test.
Re^3: Need to replace string from external file
by bhupi70007 (Novice) on Nov 06, 2017 at 11:44 UTC
    Now this is the code for replacing two arguments Sorry for the typo & other stuff i am new to this group ...
    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;
      To make you more clear my first file contain line like my name is % s My local machine ip is %d My second file contains value for %s i.e john,peter,mickey My third file contains value for %d i.e 10.0.1.2, 10.2.3.4 M output should be My name is John My name is Peter My name is mickey My local machine ip is 10.0.1.2 My local machine ip is 10.2.3.4 can you please help me on getting this output ...