8 Sep 2016
Search and Replace within a Variable
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.old | apache.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.old | mysql.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.old | postgres.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.
Invest in your career. Buy my Shell Scripting Tutorial today: