in reply to Need to replace string from external file

i have a file1 which contain text "My name is %s" I have an second file which contain mutiple values for %s replacement i.e file2 have John, peter, jessus etc Hence i need a output file that conatins output like My name is John My name is Peter My name is jessus

Sounds like a job for printf to me:

#!/usr/bin/env perl use strict; use warnings; my $text = "My name is %s\n"; my @args = qw/John peter jessus/; printf $text, $_ for @args;

File input left as an exercise.

Replies are listed 'Best First'.
Re^2: Need to replace string from external file
by bhupi70007 (Novice) on Nov 06, 2017 at 11:57 UTC
    bro hippo for your info Replacement string are in some external file :(
      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 My 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 ...
        "...make you more clear..."

        Make me clear: IP and name separated by comma or newline?

        BTW, output using sprintf as suggested by hippo using Path::Tiny:

        path($file)->spew_utf8( map { sprintf $text, $_ } @args );

        Not tested but should work. Regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help