access_time
hourglass_empty
person_outline

The Linux Sed Command: Usage and Examples

The Linux SED command is a powerful tool that helps to do general purpose tasks i.e. to parse and transform text. Nowadays, SED runs on all major operating systems.

The syntax and a number of useful features of SED are mainly borrowed from ed editor. The mechanism itself includes standard input streams as well as from text files. The script-based syntax of SED initially looks a bit difficult. However, many complex tasks can be solved with a few lines of SED script.

SED installation

In Linux based distribution, SED is installed by default. Remember, before starting, you should access your VPS with SSH. Check out our PuTTY tutorial if you’re having trouble. The which command can be used to check whether the system has an SED or not. A GNU/Linux Debian can install SED using apt package manager as follows:

[user]: ~$ sudo apt-get install sed

In order to ensure the SED is installed successfully, the following command is used:

[user]: ~$ sed --version

The command returns the following output:

sed (GNU sed) 4.2.2
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
 
Written by Jay Fenlason, Tom Lord, Ken Pizzini,
and Paolo Bonzini.
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <bug-sed@gnu.org>

SED Workflow

The basic workflow of SED is Read, Execute and Display as mentioned in Figure 1.

The read command takes input value and stores in a pattern buffer. The execute part sequentially executes the command on a file. After the execution, it displays the output stream. Pattern buffer is set free as the content is displayed on the interface. The read, execute and display command process repeats until the end of the file.

SED Example

To put it simply SED reads a file and outputs its text. To create the text file, use the vi editor.

[user]: ~$ vi text.txtPrograms must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer Programs

The command to read a text file is as follows:

[user]: ~$ sed ‘’ text.txtPrograms must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer Programs

The command takes input from the file “text.txt”. Before uploading the input file, write a command line argument in a pair of single quotes. This “tells” the command line to execute SED.

SED reads the input file “text.txt” and stores the data in a pattern buffer. After that, the operation can be performed.

In this case, we pass an empty argument so no operation is performed. SED displays the stored data as standard output and empties its pattern buffer.

[user]: ~$ sed ‘’Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer ProgramsPrograms must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer Programs

The SED ‘’ command takes input from the keyboard. It is displayed on the first and second line. It displays the data stored in the pattern buffer. To close the SED session use CTRL+D keyboard input.

Basic commands

Let’s learn the basic uses of the Linux SED command:

Delete command

To perform the delete command, use d together with a file within quotes. The command will delete the first line in text.txt file

[user]: ~$ sed ‘1d’ text.txt

Write Command: To perform the write command, write w, the line number, and the file, within quotes. The following command reads the second line and writes it in the text2.txt file.

[user]: ~$ sed '2~2 w text2.txt' text.txt  
[user]: ~$ cat text2.txt
 
Harold Abelson, Structure and Interpretation of Computer Programs

Append Command

Use the keyword and a line number within quotes. After closing the quotes, provide the appended source. The following command appends after the second line in the text.txt file.

[user]: ~$ sed '2 a The Append example' text.txt[user]: ~$ cat text.txtPrograms must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer ProgramsThe Append example'

Read Command

Use r and type in the file location within quotes. The following command will read the input from a text file and append after the third line in the text2.txt file.

[user]: ~$ sed '3 r text.txt' text2.txt
[user]: ~$ cat text2.txt
 
Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer Programs
The Append example'
Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer Programs

Conclusion

In this tutorial, you learned about SED installation and basic operations. The SED utility allows the user to use the command line in multiple ways.

It helps to solve complex problems in an efficient way. Not only SED the GNU- Linux provides many useful utilities to perform day to day tasks.

The Author

Author

Laura Z. / @laurazelvyte

Related tutorials

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!

More in VPS
How to Install phpMyAdmin on Ubuntu 18.04
Close