Editing Streams with sed

Learn how to edit streams and achieve the find and replace functionality using the sed command.

The sed command

If we need to modify text in a pipeline or a file, sed is our best friend. Its name is short for “stream editor,” and it’s convenient. While we can do many things with sed, the most common use is to replace some text with other text, similar to how we use the find and replace feature in our text editor.

Like other tools, sed can read its input from a file or standard input. Let’s try it out. Print out Hello World and use sed to replace Hello with Goodbye:

$ echo "Hello World" | sed -e 's/Hello/Goodbye/'

In some versions of sed, the expression must be preceded by -e to indicate that an expression follows. The s stands for “substitute,” while the g stands for “global,” which means that all matching occurrences in the line would be replaced.

Use the terminal below to practice these commands.

Get hands-on with 1200+ tech skills courses.