troubleshooting Question

Perl replace text and save file

Avatar of bt707
bt707Flag for United States of America asked on
Perl
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:
------------------------------------------------
#!/usr/bin/perl
use strict;

my $file = "pass.pl";
my $Npass;

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

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

------------------------------------------------

Example:

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

AdminPasswd:xxxyyy

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'

Thanks,




ASKER CERTIFIED SOLUTION
zlobcho

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