Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 26, 2023 06:07 pm GMT

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.

Youll see how, in just a bit. Lets 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.

The letter s stands for substitute.

The letter g stands for globally.

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 doesnt 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 youre unfamiliar with Regex, dont worry, Ill 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.

Lets see a simple scenario below:

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

name: James Bond

Now I need to update it as shown below:

The name is Bond, James Bond.

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

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

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.
Its 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, its all SED and done now.

Congratulations!!

Weve 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.


Original Link: https://dev.to/cloudsky13/eliminate-the-monotony-automation-with-sed-4ond

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To