string substitution in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting string substitution in perl
# 1  
Old 02-10-2009
string substitution in perl

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.
# 2  
Old 02-10-2009
i'm not a perl monk . . . but this works:

Code:
#!/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";
  }


Last edited by quirkasaurus; 02-10-2009 at 02:35 PM.. Reason: clean up...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

COBOL: Substitution in string

We have a formatted screen system where a driver program passes the locations of a list of files that called programs may be using. It will look something like this: /{number of characters varies}/DATA/MASTERFILEBecause of the size of some files we will be splitting older records into a history... (2 Replies)
Discussion started by: wbport
2 Replies

2. Shell Programming and Scripting

Perl substitution

Hi, I'm new to Perl, and I want to change a few columns in a file in order to insert them into a database. The input file looks like this: 00001,"01/1234567" ,"Tst2" 00002,"01/4545646" ,"Tst123456" 00003,"01/8979898" ,"" The output should look like this: 01-1234567,00001... (2 Replies)
Discussion started by: Subbeh
2 Replies

3. Shell Programming and Scripting

perl substitution

Hello all I have a strings like " Watch news 24x7 "."x-wars is glowing" " Watch news like 24 x 7"."x-mas will be celebrated" " Dimensions of box is 24x23x47 ". I have to remove the x(by) in between the number. If i just replace x, it will also remove all x's from text which i do not want.... (1 Reply)
Discussion started by: vasuarjula
1 Replies

4. Shell Programming and Scripting

substitution using perl

Hi All, I need the perl version of the below sed command: sed 's/abc.*/&.txt/g' <filename> Because I'm trying to do some replacement recursively using perl and the above replacement is replacing the abc* with "&.txt" exactly. Thanks, Arun (9 Replies)
Discussion started by: arun_maffy
9 Replies

5. Shell Programming and Scripting

Perl:string substitution Pattern: ='abc...',

Hi friends, I want to substitute "a ='....'," with ":" in everywhere in a string using Perl. Details: ---------- my $str= " c1='fgfasfgasggfgff.,akhkhahha', c2='bbbn', c3='hg5 sh' "; Required o/p: $str= " c1:c2:c3 " I tried as below: $str=~ s/=\'.*\',/:/g ; print "str=... (14 Replies)
Discussion started by: Niroj
14 Replies

6. Shell Programming and Scripting

String substitution

Hi, I have a properties file (myprop.properties) which contains some values: @oneValue@==tcp://localhost:1234 @twoValue@==tcp://localhost:4563 @threeValue@==tcp://localhost7895 I have a xml file (myXmlFile.xml)which contains some tokens: <application name="aTest"> <NameValuePair> ... (3 Replies)
Discussion started by: ctrl-alt-del
3 Replies

7. Shell Programming and Scripting

string substitution

Hey ppl, Could u tell me how to replace such a string xyz->x with XYZ(x), where x can be any variable accessible by pointer to structure, xyz in an entire file? (3 Replies)
Discussion started by: laxmi
3 Replies

8. UNIX for Advanced & Expert Users

String Substitution

Hey ppl, Could u tell me how to replace such a string xyz->x with XYZ(x), where x can be any variable accessible by pointer to structure, xyz in an entire file? (1 Reply)
Discussion started by: laxmi
1 Replies

9. Shell Programming and Scripting

Perl Substitution

I have lines in a file that look like this: machine: machinea machine: machineb machine: randomwhatevermachine I want to replace the machine lines with: machine: machinec I tried perl -pi -e "s/#machine:\?*/machine: machinec/" filename But this ended up doing this: ... (2 Replies)
Discussion started by: Lindarella
2 Replies

10. UNIX for Dummies Questions & Answers

Sed String Substitution

Hi! I've the following script code with an input parameter: sed 's/oldstring/$1/g' myfile > newfile (I launch it with comman line: $ MyShell newstring) Problem: the substituion doesn't work (oldstring becomes $1, instead of newstring). How could I solve this situation? Thanks, ... (2 Replies)
Discussion started by: pciatto
2 Replies
Login or Register to Ask a Question