Perl replace text and save file

I have some scripts that has a password hard coded in them and need to make a script that I can replace them with.

Not sure how to do what I need to do, I can do it with a one line command to replace the word but I need to do this in a script and not sure how.

What I need to do is to run a small script where it will ask for a new password to enter then replace the password in the file, make a backup copy of the file then save the file.

So something to start with like:
use strict;

my $file = "";
my $Npass;

print "Enter a New Password: ";
my $Npass = <STDIN>; chomp $Npass;

$r = s/xxxyyy/$Npass/g;



If is have a file named and there is a line in it such as:


Then I can modify the to replace the xxxyyy with the new password given.

I'm just not sure how to open the file to edit then save it to a new copy from doing this in a script, on command line I can just do a perl -p -i -e 's/xxxyyy/NewPassWord/g'


Who is Participating?
zlobchoConnect With a Mentor Commented:

use strict;
my $file=$ARGV[0];
#my $file = "";
print "Enter a New Password: ";
my $Npass = <STDIN>;
chomp $Npass;
open (DATA,"$file") or die "can not open the inputfile\n";
   open (FILE,">>new_$file") or die "can not create a new file\n";
   if ($_!~/AdminPasswd:(.*)/){
    print FILE $_;
   if ($_=~/AdminPasswd:(.*)/){
       print FILE $_;
       close (FILE);
close (DATA);

Open in new window

bt707Author Commented:

Thank you very much, that worked Great.

Thanks again!!
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.