08 Oct 2015
October 8, 2015

Using sed to replace multiple variables

October 8, 2015 Blog, Scripting 0 Comment

In this article I show how to build a search and replace script that uses an array to pass mutiple find/replace arguments to single sed execution.

If I wanted to create a bash script that replaces text within a file i.e. replace placeholder variables in a file with values from other variables, I would do some thing like this.

Lets create a script called sedtest.sh with the following contents

What this script will do is read the file called

this ths source file we have for eample:

TYPE=@@TYPE@@
#WebSphere Deployment Manager Properties
#This file is used to pass variables to the main install script
UNIQUE_CELL_NAME=@@CELL_NAME@@

The is will create an array of sed replacement commands ie load the two seperate indivdual search and replace argument, then it wil apply the global sed find and replace on this file and then the result of the action will be redirected to a file called.

The result being:

TYPE=myType
#WebSphere Deployment Manager Properties
#This file is used to pass variables to the main install script
UNIQUE_CELL_NAME=mycellName

Note: This is just an example, and could be much more complex, but ti does the job. If you were to add indirection, you could create valiables that hold variable names and iterate an array of variables and process the search and replace as required.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply