Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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 ( [id://1179532]=note: print w/replies, xml ) Need Help??


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

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.

  • Comment on Re: 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^2: read the value of variable from environment file and set it to another properties file using perl script
by vishallearningperl (Initiate) on Jan 13, 2017 at 17:43 UTC

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1179532]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found