Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

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 = "pass.pl";
my $Npass;

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

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



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


Then I can modify the script1.pl 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'


Avatar of zlobcho
zlobchoFlag of Germany image

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

This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answers