Solved

Inserting strings within files...

Posted on 2006-07-09
19
300 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
Comment Utility
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
Comment Utility
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
Comment Utility
#!/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
 
LVL 4

Expert Comment

by:NorCal2612
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
They're all in the same directory:

c:\ssn4\ssn4.txt
c:\ssn4\ssn4.bat
c:\ssn4\mrid.txt
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Assisted Solution

by:NorCal2612
NorCal2612 earned 350 total points
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Ok...
0
 

Author Comment

by:isaacr25
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now