DEV Community

Babak K. Shandiz
Babak K. Shandiz

Posted on • Originally published at babakks.github.io on

1

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'

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

ℹ️ 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.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay