The Shell Scripting Tutorial


Editing Contents of Variables

Search and Replace within a Variable

8 Sep 2016

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.


My Paperbacks and eBooks

My Shell Scripting books, available in Paperback and eBook formats. This tutorial is more of a general introduction to Shell Scripting, the longer Shell Scripting: Expert Recipes for Linux, Bash and more book covers every aspect of Bash in detail.

Shell Scripting Tutorial

Shell Scripting Tutorial
is this tutorial, in 88-page Paperback and eBook formats. Convenient to read on the go, and in paperback format good to keep by your desk as an ever-present companion.

Also available in PDF form from Gumroad:Get this tutorial as a PDF
Shell Scripting: Expert Recipes for Linux, Bash and more

Shell Scripting: Expert Recipes for Linux, Bash and more
is my 564-page book on Shell Scripting. The first half covers all of the features of the shell in every detail; the second half has real-world shell scripts, organised by topic, along with detailed discussion of each script.