troubleshooting Question

Perl replace text and save file

Avatar of bt707
bt707Flag for United States of America asked on
2 Comments1 Solution388 ViewsLast Modified:
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'



Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros