Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

How to edit streams of text using the sed command

Adnan Abbas

The sed command stands for stream editor in UNIX-based systems. It performs text editing operations on text coming from the standard input or a file. It can perform operations such as deletion, searching, find and replace, or insertion in a file even without opening it. All the editing decisions are executed when calling the command, which is a powerful way to transform text, especially as part of an automated workflow.

Syntax

sed [options] commands [input file] 

Since sed works on an input stream or a text file, it means that we can send the output of some other command directly to sed.

sed outputs everything on the standard output (STDOUT) by default. If we want to save the output in a file, we can redirect the output produced by sed.

Examples

The sed command provides a wide range of functionalities. We will be exploring some of the functions such as printing, deleting, saving, and replacing text in a text file. For this purpose, we have provided the terminal window below for you to execute the commands.

First, we need to create a text file on which we will be doing manipulations.

Follow this sequence create a new file:

  • cat > example.txt
  • Write some text after pressing enter. You can move to a new line by pressing enter. After you are done writing, save the file by pressing Ctrl+D.

For our examples, we will be using the following text:

'Windows is an operating system.

Windows provides excellent security.

Windows is a product of Microsoft. And Windows is paid.’

You can copy this text and paste into the terminal after the cat > example.txt command.

Running sed commands

So now that our file is ready we can dive into running some sed commands.

Printing the file

sed sends its output to the standard output by default. So we can view the contents of the file by passing it no editing commands at all. In the following command, we leave the editing commands field empty:

sed '' example.txt

We can also print the contents by providing the standard input rather than the file.

cat example.txt | sed ''

Manipulating print

We can use the p command to explicitly print the contents of the stream. In addition to that, we can use the n option to surpass automatic printing on the standard output. This allows us to target specific portions of the text.

This command will allow us to print the first two lines of the text file:

sed -n '1,2p' example.txt

In this example, we’ve told sed to print lines 1 through 5.

Deleting and saving Text

We can delete the contents of the file using the d command. sed will print everything that is not deleted as a result of this operation.

sed '1d' ed.txt

The command above deletes the first line and prints the remaining contents of the file.

It is important to note that the delete operation does not modify the source file. In order to save the changes made, we can redirect the output to a file in this way:

sed '1d' example.txt > example1.txt

Substituting text

sed is known for its powerful capabilities to replace text. It can search for text patterns using regular expressions and then replace the found text with the desired value.

In its most basic form, we can change one word to another word using the following syntax:

's/old_word/new_word/'

The s is the substitute command. The three slashes (/) are used to separate the different text fields.

Let’s replace the word Windows by Linux in our example text file using the sed command:

sed 's/Windows/Linux/' example.txt

This command will replace the first word encountered in the line with the desired one in each line. To replace all the word occurrences in a line, we use the g (global) option.

sed 's/Windows/Linux/g' example.txt

Terminal 1
Terminal

RELATED TAGS

CONTRIBUTOR

Adnan Abbas
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring