O'Reilly logo

Linux Shell Scripting Cookbook by Sarath Lakshman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Basic sed primer

sed stands for stream editor. It is a very essential tool for text processing. It is a marvelous utility that can play around regular expressions. A well-known usage of the sed command is for text replacement. This recipe will cover most of the frequently used sed techniques.

How to do it…

sed can be used to replace occurrences of a string with another string in a given text. It can be matched using regular expressions.

$ sed 's/pattern/replace_string/' file

or

$ cat file | sed 's/pattern/replace_string/' file

This command reads from stdin.

To save the changes along with the substitutions to the same file, use the -i option. Most of the users follow multiple redirections to save the file after making a replacement as follows:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required