in reply to Re: read the value of variable from environment file and set it to another properties file using perl script
in thread read the value of variable from environment file and set it to another properties file using perl script

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

  • Comment on Re^2: read the value of variable from environment file and set it to another properties file using perl script

Replies are listed 'Best First'.
Re^3: read the value of variable from environment file and set it to another properties file using perl script
by vishallearningperl (Initiate) on Feb 01, 2017 at 10:27 UTC

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