Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Inserting strings within files...

Posted on 2006-07-09
19
Medium Priority
?
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1400 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 400 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 400 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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

609 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