RSS Feed Share on Twitter

All Shell Scripting Tips

8 Sep 2016

Search and Replace within a Variable

find and replace within variable name

Isn't it a pain to have to get the contents of a variable, edit it, and put it into another variable?

For example, to update a file's name and contents to a new format, such that given three input files, apache.old, mysql.old and postgres.old, the script should transform them as shown, and rename them from .old to .new:

apache.oldapache.new
This is the apache.old file
this is the old thing
blah blah etc
This is the apache.new file
this is the new thing
blah blah etc
mysql.oldmysql.new
This is the mysql.old file
this is the old thing
blah blah etc
This is the mysql.new file
this is the new thing
blah blah etc
postgres.oldpostgres.new
This is the postgres.old file
this is the old thing
blah blah etc
This is the postgres.new file
this is the new thing
blah blah etc

A classic approach would be as script like this, which transforms the filename, uses sed to update the contents, and write the result to the new file:

#!/bin/bash
for oldfile in *.old
do
  # Replace ".old" ending with ".new" for the new file
  newfile=`echo $oldfile | sed s/".old"/".new"/g`
  echo "Updating ${oldfile}..."
  # replace "old thing" with "new thing" for the new version, and write to .new
  sed s/"old thing"/"new thing"/g $oldfile > $newfile
done

For the purposes of just editing a file to a new version, there is nothing wrong with this approach. But there is a better way, which saves the need to create the extra variable for this one-time use, and makes far more easily readable code:

#!/bin/bash
for oldfile in *.old
do
  echo "Updating ${oldfile}..."
  # replace "old thing" with "new thing" for the new version, and write to .new
  sed s/"old thing"/"new thing"/g $oldfile > ${oldfile/.old/.new}
done

The use of ${oldfile/.old/.new} tells Bash to take the value of $oldfile, such as "apache.old", and replace any occurrences of ".old" within it to ".new".

You will probably have noticed that ${oldfile/.old/.new} is also very similar to the syntax in the sed s/"old thing"/"new thing"/g search-and-replace method used to change "old thing" to "new thing" within the file.

 

 


You can buy the content of this tutorial as a PDF to download to all of your devices!

Contact

You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid. Don't forget to include the simple addition question at the end of the form, to prove that you are a real person!