vishallearningperl has asked for the wisdom of the Perl Monks concerning the following question:

I want to read the value of variable from environment file and set it to another properties file using perl script.

Environment File

DOMAIN=<value> DOMAIN_TEST_DB_CONN_STR=<value> DOMAIN_TEST_DB_USER=<value> DOMAIN_TEST_DB_PSWD=<value>

Properties File

#These are the properties for engineering #We expect you to change them for your environment # #CustomerDatabase info DOMAIN=<value> DOMAIN_TEST_DB_CONN_STR=<value> DOMAIN_TEST_DB_USER=<value> DOMAIN_TEST_DB_PSWD=<value>

I tried following but not working. Kindly please suggest.

$envfile="/root/env.properties"; @envFile=<$env>; open my $env, '<', $envfile or die "Can't read old file: $!"; open my $in, '<', $file or die "Can't read old file: $!"; open my $out, '>', "$file.new" or die "Can't write new file: $!"; print "\nFile contents:"; print @envFile; foreach $envline (@envFile){ while ( <$in> ){ print "$. $_"; if($envline =~/$DOMAIN_DB_CONN_STR=.*:1521:.*/ && $_=~/$DOMAIN +_DB_CONN_STR=.*:1521:.*/){ print "\nMatch"; $line=$_; $line =~ s/$DOMAIN_DB_CONN_STR=.*:1521:.*/NUANCE_DB_CO +NN_STR=$dbhost:$dbport:$dbschema/; print $out $line; } } }
  • Comment on read the value of variable from environment file and set it to another properties file using perl script
  • Select or Download Code

Replies are listed 'Best First'.
Re: read the value of variable from environment file and set it to another properties file using perl script
by choroba (Cardinal) on Jan 13, 2017 at 16:00 UTC
    $envline =~/$DOMAIN_DB_CONN_STR=.*:1521:.*/ && $_=~/$DOMAIN_DB_CONN_ST +R=.*:1521:.*/){ # ~ ~

    Do you use strict? What do you think the dollar signs mean in a regular expression when followed by letters?

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Hi I have not used strict. I used $DOMAIN ans I have different values for $DOMAIN and I am using same regex for them. e.g. FINANCE_DB_CONN_STR, MARKETING_DB_CONN_STR

Re: read the value of variable from environment file and set it to another properties file using perl script
by kennethk (Abbot) on Jan 13, 2017 at 16:51 UTC
    First, thank you for providing clear examples. It makes debugging profoundly easier.

    To understand what strict is, see Use strict warnings and diagnostics or die. Several of the bugs in this code would have been immediately apparent if you used this tool.

    $envfile="/root/env.properties"; @envFile=<$env>; open my $env, '<', $envfile or die "Can't read old file: $!";
    You're trying to read in your file before you've opened your file handle. Therefore the array is empty, and you never execute anything in your foreach loop.
    open my $in, '<', $file or die "Can't read old file: $!"; open my $out, '>', "$file.new" or die "Can't write new file: $!";
    You never initialize the value for $file. I'm very surprised you aren't dying right here.
    print "\nFile contents:"; print @envFile; foreach $envline (@envFile){ while ( <$in> ){
    Not indenting this line makes following flow challenging.
    print "$. $_"; if($envline =~/$DOMAIN_DB_CONN_STR=.*:1521:.*/ && $_=~/$DOMAIN +_DB_CONN_STR=.*:1521:.*/){
    You never initialize $DOMAIN_DB_CONN_STR, so the regular expression will actually check /=.*:1521:.*/, which is presumably not what you want.
    print "\nMatch"; $line=$_; $line =~ s/$DOMAIN_DB_CONN_STR=.*:1521:.*/NUANCE_DB_CO +NN_STR=$dbhost:$dbport:$dbschema/;
    Same thing for $dbhost, $dbport, and $dbschema.
    print $out $line; } } }
    You appear to intend to check every line against every other line, but this would only execute for the first line of the $env file because you don't reset/rewind the $in file. You could either rewind (seek) or pull the $in file into an array as well.

    Following the extra input you gave in Re^2: read the value of variable from environment file and set it to another properties file using perl script and rewriting this in an inefficient but easy-to-follow manner, you might have:

    use strict; use warnings; my $envfile = "/root/env.properties"; my $file = 'input.txt'; # Or whatever open my $env, '<', $envfile or die "Can't read old file: $!"; open my $in, '<', $file or die "Can't read old file: $!"; open my $out, '>', "$file.new" or die "Can't write new file: $!"; my @envFile = <$env>; my @inFile = <$in>; print "\nFile contents:"; print @envFile; my $dbhost = ''; my $dbport = ''; my $dbschema = ''; foreach my $envline (@envFile){ my $inLineCount = 0; foreach my $inline (@inFile){ $inLineCount++; print "$inLineCount $inline"; foreach my $DOMAIN_DB_CONN_STR ('FINANCE_DB_CONN_STR', 'MARKET +ING_DB_CONN_STR') { if($envline =~/$DOMAIN_DB_CONN_STR=.*:1521:.*/ && $inline +=~/$DOMAIN_DB_CONN_STR=.*:1521:.*/){ print "\nMatch"; my $line = $inline; $line =~ s/$DOMAIN_DB_CONN_STR=.*:1521:.*/NUANCE_DB_CO +NN_STR=$dbhost:$dbport:$dbschema/; print $out $line; } } } }

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks a lot. I tried code suggested by you and it worked very well. I will also go through use of strict and related articles.

      Sorry for confusion caused by copying just snipped of code. Yes I had declared envFile and env variables above the code i copied and was asking user to enter inputs required.

      Thanks again for great help

        I have one more query on this.

        In following Environment File I am giving only values of parameters which would be replaced in properties file

        DOMAIN=<value> DOMAIN_TEST_DB_CONN_STR=<value> DOMAIN_TEST_DB_USER=<value> DOMAIN_TEST_DB_PSWD=<value> [download]

        Followin Properties File have other lines too which do not match those from environment files above.

        #These are the properties for engineering #We expect you to change them for your environment # #CustomerDatabase info DOMAIN=<value> DOMAIN_TEST_DB_CONN_STR=<value> DOMAIN_TEST_DB_USER=<value> DOMAIN_TEST_DB_PSWD=<value>

        My objective is to replace values of properties file based on environment file at same time keeping non matching lines as it is. If I use the following code, duplicate lines will be printed for every line from environment file.

        use strict; use warnings; my $envfile = "/root/env.properties"; my $file = 'input.txt'; # Or whatever open my $env, '<', $envfile or die "Can't read old file: $!"; open my $in, '<', $file or die "Can't read old file: $!"; open my $out, '>', "$file.new" or die "Can't write new file: $!"; my @envFile = <$env>; my @inFile = <$in>; print "\nFile contents:"; print @envFile; my $dbhost = ''; my $dbport = ''; my $dbschema = ''; foreach my $envline (@envFile){ my $inLineCount = 0; foreach my $inline (@inFile){ $inLineCount++; print "$inLineCount $inline"; foreach my $DOMAIN_DB_CONN_STR ('FINANCE_DB_CONN_STR', 'MARKET +ING_DB_CONN_STR') { if($envline =~/$DOMAIN_DB_CONN_STR=.*:1521:.*/ && $inline +=~/$DOMAIN_DB_CONN_STR=.*:1521:.*/){ print "\nMatch"; my $line = $inline; $line =~ s/$DOMAIN_DB_CONN_STR=.*:1521:.*/NUANCE_DB_CO +NN_STR=$dbhost:$dbport:$dbschema/; print $out $line; } else { print $out $line; } } } }

        Output would be

        #These are the properties for engineering #We expect you to change them for your environment # #CustomerDatabase info DOMAIN=<value> DOMAIN_TEST_DB_CONN_STR=<value> DOMAIN_TEST_DB_USER=<value> DOMAIN_TEST_DB_PSWD=<value> #These are the properties for engineering #We expect you to change them for your environment # #CustomerDatabase info DOMAIN=<value> DOMAIN_TEST_DB_CONN_STR=<value> DOMAIN_TEST_DB_USER=<value> DOMAIN_TEST_DB_PSWD=<value> #These are the properties for engineering #We expect you to change them for your environment # #CustomerDatabase info DOMAIN=<value> DOMAIN_TEST_DB_CONN_STR=<value> DOMAIN_TEST_DB_USER=<value> DOMAIN_TEST_DB_PSWD=<value> #These are the properties for engineering #We expect you to change them for your environment # #CustomerDatabase info DOMAIN=<value> DOMAIN_TEST_DB_CONN_STR=<value> DOMAIN_TEST_DB_USER=<value> DOMAIN_TEST_DB_PSWD=<value>

        Could you please let me know how to handle this?

        Thanks in advance!!