Replacing text in a file from a shell script

while automating a flow, it could come in handy to replace sometimes some text or the complete line in a configuration file. This can de done easely on your mac via a shell script with the use of the sed command.

An example

Suppose you have following CHANGELOG.md and want to replace the text [upcoming release] with an actual version number instead.

Image

CHANGELOG.md

# [upcoming release]
- fixed flow issue

# 1.1.0
- fixed layout issue

# 1.0.0
- initial release

Following command will update the text [upcoming release] with 2.0.0.

sed -i -e 's/\[upcoming release\]/2.0.0/g' CHANGELOG.md

In order to automate it even further, you could create a shell script that uses one input argument, the version number.

Automation via shell script

Create a file with name update-version-number.sh and following content:

#!/bin/bash
VERSION_NUMBER=$1
sed -i -e "s/\[upcoming release\]/$VERSION_NUMBER/g" CHANGELOG.md

Now you can run the command by executing sh update-version-number.sh 2.0.0.

RTFM

If you need more information about this command, consult the manual.

man sed