Pectus Excavatum Pectus Excavatum - 1 month ago 9
Bash Question

Find and replace text using a bash script

I have the following script which changes a password using a password file that I have piped the password to in the correct format. However, the next command needs a password file with a revised format. At the moment i just delete and re-create the file, but it would be nice to just edit it instead, im just not sure how:

sudo touch /tmp/password.txt
sudo chmod 600 /tmp/password.txt
sudo echo "AS_ADMIN_PASSWORD=" > /tmp/password.txt
sudo echo "AS_ADMIN_NEWPASSWORD=Pa55w0rd2" >> /tmp/password.txt
sudo /opt/glassfish3/bin/asadmin --user admin --passwordfile /tmp/password.txt change-admin-password \
--domain_name domain1
sudo rm /tmp/password.txt
sudo touch /tmp/password.txt
sudo chmod 600 /tmp/password.txt
sudo echo "AS_ADMIN_PASSWORD=Pa55w0rd2" > /tmp/password.txt
sudo /opt/glassfish3/bin/asadmin --user admin --passwordfile /tmp/password.txt enable-secure-admin

Answer

Perfect job for sed:

sed -i '/^AS_ADMIN_PASSWORD=/d' /tmp/password.txt

This deletes the line matching the given regex, in-place.