Solved

Inserting strings within files...

Posted on 2006-07-09
19
325 Views
Last Modified: 2006-11-18
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.
0
Comment
Question by:isaacr25
  • 9
  • 5
  • 3
  • +1
19 Comments
 
LVL 4

Expert Comment

by:NorCal2612
ID: 17073610
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
 

Author Comment

by:isaacr25
ID: 17073705
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
 
LVL 4

Expert Comment

by:NorCal2612
ID: 17073913
#!/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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 4

Expert Comment

by:NorCal2612
ID: 17073972
#!/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
 

Author Comment

by:isaacr25
ID: 17074082
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
 

Author Comment

by:isaacr25
ID: 17074143
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
 
LVL 39

Expert Comment

by:Adam314
ID: 17074191
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
 
LVL 4

Expert Comment

by:NorCal2612
ID: 17074211
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
 

Author Comment

by:isaacr25
ID: 17074229
They're all in the same directory:

c:\ssn4\ssn4.txt
c:\ssn4\ssn4.bat
c:\ssn4\mrid.txt
0
 
LVL 4

Assisted Solution

by:NorCal2612
NorCal2612 earned 350 total points
ID: 17074277
#!/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
 
LVL 8

Assisted Solution

by:Perl_Diver
Perl_Diver earned 100 total points
ID: 17074280
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
 
LVL 8

Accepted Solution

by:
Perl_Diver earned 100 total points
ID: 17074341
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
 

Author Comment

by:isaacr25
ID: 17074512
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
 
LVL 8

Expert Comment

by:Perl_Diver
ID: 17074780
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
 

Author Comment

by:isaacr25
ID: 17074800
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
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 50 total points
ID: 17075466
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
 

Author Comment

by:isaacr25
ID: 17075585
Ok...
0
 

Author Comment

by:isaacr25
ID: 17075626
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
 

Author Comment

by:isaacr25
ID: 17075764
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

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
perl to convert excel to csv 3 212
iSeries PERL Scripts 7 159
ppm conversion to curl on a module install 8 82
Union rows in array that have common elements (Part 2) 4 75
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question