Inserting strings within files...

Actually, the file that needs to be changed is not script1.pl. Its a .bat file: script1.bat. I don't know if that makes any difference. So:

-I have one file called file1.txt
-I have a script called script1.bat
-Within this .bat file, there is this string of text that exists at a certain location (see .bat file below): "value"
-I need a script that will take the string stored in file1.txt and insert it into script1.bat in place of "value", or any string that occupies that place.
-So, if the value in file1.txt is 1234, then I need script1.bat from "value" to "1234"


Here is what's in the .bat file:

@echo off
REM
c:\footprints\bin\perl\bin\perl c:\ssn4\ssnstrip.pl

C:\footprints\bin\perl\bin\perl C:\FootPrints\bin\mrSetField.pl %%MRID%% "Last 4 SSN" "Value" %%55%%

Thanks in advance for the help.
isaacr25Asked:
Who is Participating?
 
Perl_DiverConnect With a Mentor Commented:
chdir('c:/ssn4') or die "Can't chdir to ssn4: $!";
open(FILE, 'ssn4.txt') or die "Can't open ssn4.txt: $!";
chomp (my $value = <FILE>);
close(FILE);
open (MRID, 'mrid.txt') or die "Cannot open mrid.txt: $!";
chomp(my $mrid = <MRID>);
close(MRID);
{
   local ($^I, @ARGV) = ('.bak', 'ssn4.bat');
   while (<>) {
      s/"Value"/"$value"/;
      s/%%MRID%%/%%$mrid%%/;
      print;
   }
}
print "finished";
0
 
NorCal2612Commented:
Should work...

#!/usr/bin/perl

open(FILE, "file1.txt");
open(SCRIPT, ">>script1.bat");

my $value;

$value = <FILE>;

chomp($value);

close(FILE);

while (<SCRIPT>) { s/Value/$value/gi; }

close(SCRIPT);
0
 
isaacr25Author Commented:
NorCal2612,
     Besides being sure that the files names are correct, is there anything else that I need to change, or should I be able to use the code, as posted? I tried it and it did not work. Can you tell me what each part of the script is doing? Sorry... I'm not extremely familiar with perl. Thanks.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
NorCal2612Commented:
#!/usr/bin/perl

# opens file1.txt for reading
open(FILE, "file1.txt") || die "Cannot open file.\n";
# opens script1.bat for appending
open(SCRIPT, ">>script1.bat") || die "Cannot open script.\n";

# creates a new variable
my $value;

# assignes whatever is on the first line of file1.txt to the $value variable
# I was using a one line file containing "1234" on the first line.
$value = <FILE>;

# removes the End of Line character on the value
chomp($value);

# closes file1.txt
close(FILE);

# loops through each line of the script file and replaces all instances of the string "Value" with
# whatever is in the $value variable from above
while (<SCRIPT>) { s/Value/$value/gi; }

close(SCRIPT);

----

i just tested the replacement. it's not working as you indicated. give me a minute or two to figure out why.


0
 
NorCal2612Commented:
#!/usr/bin/perl

my $file = "test.txt";
my $script = "script.bat";

open FILE, $file;

$var = <FILE>;

chomp($var);

close FILE;

@temp;

open (SCRIPT, $script) || die "Cannot open script file.\n";

while (<SCRIPT>)
{
  s/Value/$var/gi;
  push(@temp,$_);
}

close(SCRIPT);

open (SCRIPT, ">$script") || die "Cannot open script file.\n";

print SCRIPT @temp;

close(SCRIPT);
0
 
isaacr25Author Commented:
I'm getting the error, "Cannot open script file."

Can you also please use the following file names: ssn4.txt and ssn4.bat. I don't know if I need to alter any of the names in the code you're sending, so using the real file names will help me out. Thanks.
0
 
isaacr25Author Commented:
Also,
     Sorry for adding more... I'll increase the point value. I now need to replace two values in the .bat file! I also need to replace %%MRID%% in the .bat file with the value from another file, mrid.txt. Thanks!
0
 
Adam314Commented:
Based on what you've said so far, it seems that this would be better to either be done completly in the .bat file, or completly in perl.

Is there a reason you are using perl to create a .bat file, that then calls a perl file?
0
 
NorCal2612Commented:
What are the full path names to these files:

ssn4.txt
ssn4.bat
mrid.txt

I'll put the paths in if you provide them; otherwise, the script assumes they are in the current working directory from which you are running the script which is probably why you're getting the Cannot open script error.
0
 
isaacr25Author Commented:
They're all in the same directory:

c:\ssn4\ssn4.txt
c:\ssn4\ssn4.bat
c:\ssn4\mrid.txt
0
 
NorCal2612Connect With a Mentor Commented:
#!/usr/bin/perl

my $ssnFile  = "C:\\ssn4\\ssn4.txt";
my $mridFile = "C:\\ssn4\\mrid.txt";
my $script   = "C:\\ssn4\\ssn4.bat";

open (SSN,  $ssnFile)  || die "Cannot open $ssnFile...\n";
open (MRID, $mridFile) || die "Cannot open $mridFile...\n";

$ssn  = <SSN>;
$mrid = <MRID>;

chomp($ssn);
chomp($mrid);

close SSN;
close MRID;

@temp;

open (SCRIPT, $script) || die "Cannot open $script...\n";

while (<SCRIPT>)
{
  s/Value/$ssn/gi;
  s/MRID/$mrid/gi;

  push(@temp,$_);
}

close(SCRIPT);

open (SCRIPT, ">$script") || die "Cannot open $script...\n";

print SCRIPT @temp;

close(SCRIPT);
0
 
Perl_DiverConnect With a Mentor Commented:
open(FILE, 'ssn4.txt') or die "Can't open ssn4.txt: $!";
chomp (my $value = <FILE>);
close(FILE);
{
   local ($^I, @ARGV) = ('.bak', 'ssn4.bat');
   while (<>) {
      s/"Value"/"$value"/;
      print;
   }
}
print "finished";


the above should work as long as matching "Value" is good enough. You may need more validation though if "Value" can occur in other lines that you don't want to replace with the new pattern
0
 
isaacr25Author Commented:
Ok...
    Here's a dilemma. I'm getting more and more info as time progresses. This is a process which will have to be repeated a countless number of times. So, the question is: how do I run this process repeatedly? I can't do so since after the first execution, the variable names (value, mrid) will not be there. Is there a way to have the script replace the characters at a certain location within the file, instead of looking for a variable name? If you have any ideas, I would welcome them. Thanks guys.
0
 
Perl_DiverCommented:
You could  widen the scope of the pattern matching/substitution, so instead of:

   while (<>) {
      s/"Value"/"$value"/;
      s/%%MRID%%/%%$mrid%%/;
      print;
   }

you might have something like:

   while (<>) {
      s/(C:\\footprints\\bin\\perl\\bin\\perl C:\\FootPrints\\bin\\mrSetField.pl)( %%.+?%% )("Last 4 SSN")( "\d\d\d\d" )(%%55%%)/$1 %%$mrid%% $3 "$value" $5/;
      print;
   }


you can use substr() on fixed length records.
0
 
isaacr25Author Commented:
After the first time, there will be no %%MRID%%  or "Last 4 SSN." These strings will be replaced by numbers after the first time the script runs.
0
 
Adam314Connect With a Mentor Commented:
I would create 1 .bat file that is the template.  Call this something like "template.bat", and put MRID, Value, and whatever other placeholders there are.

Then your script will open the template.bat file, do it's replace, and save it to something else, like ssn.bat.
0
 
isaacr25Author Commented:
Ok...
0
 
isaacr25Author Commented:
Ok...
    So here's where we are:

NorCal2612
     Your last set of code worked except for: the mrid value is being inserted with an extra space at the end of the value.

Adam314,
     Using the last piece of code that NorCal2612 posted, how would I accomplish using the template structure that you suggested? Thanks.
0
 
isaacr25Author Commented:
Hey guys,
    I've figured out a cleaner way to accomplish what I need. The information you provided helped a lot with coming up with the solution, so the points are doled out accordingly. Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.