Hi,
I have a template file and want to replace 3 parameters to the values that I want. these values are in a parameter file.
Any idea how to do this in perl?
the parameter file looks like:
host_name = jupiter
PORT = 1562
IPADDRESS = 10.1.34.10
the template file has lots of entry.
CX_HOST_NAME = host_name
CX_IPADDRESS = ip_address
CX__PORT = port
thanks.
i'm not a perl monk . . . but this works:
#!/usr/bin/perl
#----------------------------------------------------------------------#
# Read both files into arrays... #
#----------------------------------------------------------------------#
$param_file = "parameters.dat";
unless ( open FIN, "$param_file" ){
print "cannot open $param_file \n";
exit(1);
}
@a_parameters = <FIN>; chomp @a_parameters; close FIN;
$template_file = "template.dat";
unless ( open FIN, "$template_file" ){
print "cannot open $template_file \n";
exit(1);
}
@a_template = <FIN>; chomp @a_template; close FIN;
#----------------------------------------------------------------------#
# Peruse one array... #
#----------------------------------------------------------------------#
for $x ( 0 .. scalar( @a_parameters ) - 1 ){
( $var_name, $equals, $var_value ) = split( / /, $a_parameters[$x] );
$var_name = uc( $var_name );
#----------------------------------------------------------------------#
# Looking for values in the other... #
#----------------------------------------------------------------------#
for $y ( 0 .. scalar( @a_template ) - 1 ){
next unless ( $template[$y] = m/CX_*$var_name = / );
#----------------------------------------------------------------------#
# Substitute the template with the parameter... #
#----------------------------------------------------------------------#
$a_template[$y] =~ s/= .*$/= $var_value/;
}
}
#----------------------------------------------------------------------#
# ... and print. #
#----------------------------------------------------------------------#
for $y ( 0 .. scalar( @a_template ) - 1 ){
print "template entry " . ( $y + 1 ) . ": $a_template[$y] \n";
}