String Replace in Shell with sed [RE#4]
Though 99% of the time we use a text editor like vim or nano, still knowing how to do simple string manipulations via shell commands is more than necessary. The simplest use case emerges in CI/CD pipeline where you may need to replace some placeholder with a specific text (e.g., a dynamic docker image tag). 🤖
One simple tool for this purpose is sed. For example, to replace “[tag]” with “abcdef” execute:
echo "docker push -t app:[tag]" | sed -e 's/[tag]/abcdef/g'
You can also use sed just like grep and pass a file to be used as the input stream:
sed -e 's/[tag]/abcdef/g' input-file.txt
You may add more expressions by adding any number of -e expression arguments:
sed -e 's/[tag]/abcdef/g' -e 's/[image]/app/g' input-file.txt
ℹ️ To learn more useful applications of sed have a look at this article.
About Regular Encounters
I’ve decided to record my daily encounters with professional issues on a somewhat regular basis. Not all of them are equally important/unique/intricate, but are indeed practical, real, and of course, textually minimal.
![Godoc-Lint: a linter for Go Doc Comments (godocs) [RE#16]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1741533596295%2F731ef95a-1af8-49ae-843d-a19d9d418fdf.png&w=3840&q=75)
![Optimize Postgres Containers for Testing [RE#15]](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1741533395875%2Fe1a3c4fc-4354-4482-88b8-e8c14750e00a.png&w=3840&q=75)

