in reply to Need to replace string from external file

Hi, welcome, please show:

  1. sample input (in <code></code> tags)
  2. expected output from that input (in <code></code> tags)
  3. code you have tried (in <code></code> tags)
  4. how it fails (in <code></code> tags if it's an error message)

see PerlMonks FAQ: Posting on PerlMonks

The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Need to replace string from external file
by bhupi70007 (Novice) on Nov 06, 2017 at 11:42 UTC
    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;

      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.

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