The Linux Sed Command: Uses and Examples

The SED command is a powerful command that helps to perform a task that can have multiple purposes, such as parsing and transforming text. Today, SED runs on all major operating systems.

The syntax and a number of useful features of SED are mainly borrowed from the ed editor. The mechanism itself includes standard input streams as well as text file streams. SED’s script-based syntax may seem a bit difficult at first. However, many complex tasks can be solved with a few lines of SED commands.

SED Installation

On the Linux-based distribution, SED is installed by default. Remember, before you start, you must access your with SSH. Check our if you have problems with this. The which command can be used to check whether the system has SED installed or not. On GNU/Linux Debian system the SED command can be installed using the apt package manager as follows:

: ~$ sudo apt-get install sed

To ensure that the SED command has been installed correctly, you can use the following command:

: ~$ sed –version

The command will return the following output:

sed (GNU sed) 4.2. Copyright (C) 2012 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . 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: . General help using GNU software: . E-mail bug reports to:

SED workflow

The basic workflow of SED is Read, Execute and Show, as we saw in the Figure 1.

See also  10 WordPress RSS Feed Plugins to Easily Share the Latest Content

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

SED Usage Examples

In a nutshell, SED reads a file and outputs text as output. To create the text file, use the vi editor.

: ~$ 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:

: ~$ 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 loading the input file, you must enclose a command line argument between the pair of single quotes. This “tells” the command line to run SED.

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

Also, you can write an empty argument so that no operation is performed. SED displays the stored data to standard output and flushes its pattern buffer.

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

See also  Register Your .XYZ Domain For Only €1.99 |

The command SED takes input from the keyboard. They are displayed on the first and second lines. Shows the data stored in the pattern buffer. To close the SED session, use the keyboard input CTRL+D.

Basic SED Commands

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

delete command

To run the delete command, use d along with a file in quotes. The command will remove the first line in the text.txt file:

: ~$ sed ‘1d’ text.txt

write command

To run the write command, type w, the line number, and the file, in quotes. The following command reads the second line and writes it to the file text2.txt:

: ~$ sed ‘2~2 w text2.txt’ text.txt : ~$ cat text2.txt Harold Abelson, Structure and Interpretation of Computer Programs

add command

Use the keyword and a line number in quotes. After closing the quotes, provide the attached source. The following command displays the second line of the text.txt file.

: ~$ sed ‘2 a The Append example’ text.txt: ~$ cat 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 Append example’

read command

Uses r and enclose the file location in quotes. The following command will read the input from a text file and add it after the third line in the file text2.txt.

: ~$ sed ‘3 r text.txt’ text2.txt : ~$ 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

See also  VPS - Virtual Server In Spain |

conclusion

With this tutorial, we hope that you have learned how to install the SED command and its basic operations. The SED tool allows the user to use the command line in multiple ways.

Helps solve complex problems efficiently. Not just SED, GNU-Linux provides many useful benefits to perform day-to-day tasks.

Deyi is a digital marketing enthusiast, with a background in web design, content creation, copywriting, and SEO. She is part of ‘s SEO & Localization team. In her free time, she likes to develop projects, read a book or watch a good movie.

Loading Facebook Comments ...
Loading Disqus Comments ...