DEV Community

cloudsky13
cloudsky13

Posted on • Updated on

Eliminate the Monotony: Automation with sed.

INTRODUCTION

Sed, short for “Stream Editor” has been around for a while, generally used to replace specific string values in a file with another.

Well, that sounds straight forward, why not use the replace function available in text editors though?

Of course, you can, but automating this simple task makes it much more efficient, especially in case you need to update a bunch of files.

You’ll see how, in just a bit. Let’s get you familiar with the syntax first.

Syntax

The syntax is pretty basic:

sed 's/pattern/replacement/g' <filename>

The above command replaces pattern with replacement in
the file and prints the output in console.

Here,
s stands for substitute.
g stands for globally.
And the / is a delimiter used as a field separator.

The above syntax is mostly used to view the output of the
sed command before implementing it.

sed -i 's/pattern/replacement/g' <filename>

The -i option tells sed to update the file in place. You
can check out other options available with sed using sed -
-help
.

TIP

If your pattern or replacement contains /, like in a URL, there can be undesired outputs or errors too.

To avoid that, you can simply use \\ before the /. This treats the / as a normal character and not a delimiter.

PRO TIP

You can use any other character as a delimiter as long as it doesn’t appear in the pattern or replacement.

For example:
sed -i 's|pattern|replacement|g' <filename>, works perfectly fine.

Advanced Usage: Capture Groups in sed

Capture groups are used to define portions of a regular expression (Regex) that can be captured and stored for later use.

If you’re unfamiliar with Regex, don’t worry, I’ll cover the example used here. You can read more about it here

Capture Groups are defined using parenthesis () which enclose the text/pattern to be captured.

Let’s see a simple scenario below:

Say I have a file sample.txt with the below data:

name: James Bond
Enter fullscreen mode Exit fullscreen mode

Now I need to update it as shown below:

The name is Bond, James Bond.
Enter fullscreen mode Exit fullscreen mode

One can achieve the desired state using sed command in combination with Regex:

sed -i 's/\(.*\): \(.*\)/The \1 is Bond, \2./g'  sample.txt
Enter fullscreen mode Exit fullscreen mode

Comparison with standard syntax:

sed -i 's/pattern/replacement/g' <filename>

pattern = \(.*\): \(.*\)

\(.*): collects all data present before : and maps it to \1 , (i.e. \1=“name”.)

\(.*\) collects all data present after : and maps it to \2 , (i.e. \2=“James Bond”.)

replacement = The \1 is Bond, \2.

CONCLUSION

• Sed command is lightweight and efficient.
• It’s compatibility with Regex makes complex text manipulation eazy-peezy lemon squeezy.
• It provides the option of in-place editing instead of creating a new copy.
Easy integration with automation scripts, eliminating the monotony.

Well, it’s all SED and done now.

Congratulations!!

We’ve successfully scratched the surface of the above "said" command. (Pun-o-meter at 2 now)

But I hope this blog post enables you to envision some real-life use cases of sed. So, tell me...

What could be a good problem statement to solve using sed?

(Cue for another blog? May be…)

Thanks for making it till the end. Drop a feedback below.

Top comments (0)