Sed is a stream editor that analyses and transforms input on operating systems that resemble Unix. In the following, you will learn the details of Sed commands in Linux.
Table of Contents
SED Commands in Linux: Detailed Analysis
The stream editor of the Sed commands in Linux makes basic text alterations to supplied data (an input or a file from a pipeline). Although sharing some similarities with editors that support programmed changes (like ed), Sed is more effective since it simply makes one run over the input(s). But what sets Sed Commands apart from different types of editors is its capacity to analyze text in a pipeline.
Identifying and altering material in a file is quicker with SED compared to the VI Editor since it lets you change files regardless of whether you are viewing them.
The SED text stream editor is a potent tool. It can insert, remove, search for, and modify (substitution).
Regular expression support in the SED command of Unix enables it to do intricate pattern recognition.
Structure of SED Command
- The primary syntax for the Sed commands in Linux is as follows:
Sed OPTIONS… [SCRIPT] [INPUT FILE…]
The sed syntax consists of three components.
- Options govern the Linux command’s output.
- A collection of Linux commands that run is included in the script.
- The file on which you run the sed command is represented by the file name (with extension).
A sed command in Linux can be executed without inputs. The script can also be started without even a filename. In such a scenario, it uses the standard input data.
Variety of Options
- One may use the following command-line parameters to run Sed commands in Linux:
-b, –binary Allow lines to terminate at a line feed by opening input data in binary mode. –follow-symlinks If the supplied document is a symbolic link, modify the destination file only if you employ the -I option to do that function. –debug To display the input in its parametric form and describe program execution, switch to the debug mode. -z, –null-data, –zero-terminated Consider input as a collection of lines, each of which terminates in a zero byte. –sandbox Limit the use of external applications and limit command-line operations to input files alone. -s, –separate Display the given files separately rather than as a single, endless stream. -u, –unbuffered reduce the buffers for input and output.
Examples of Sed commands in Linux
Example 1: Here, we demonstrate two different global substitutions. With a worldwide substitute, we use s to substitute another pattern (XYZ) for every instance of a way (ABC).
To do a global replacement, we append the /g prefix. Moreover, one may choose to use /ng to carry out a global replacement beginning with the n-th instance of the pattern. The two cases are shown below, in which we have assumed n to be “3” for the second scenario:
Using /g changed every occurrence of “ABC” with “XYZ,” but using /3g just substituted instances up to the third instance.
Example 2: Under this instance, we use s to substitute another pattern (z) for the third sequence instance (ABC). Similar substitutions may be made for the nth occurrence of an arrangement by substituting the number n for 3 in the illustration below:
Notably, z has only replaced ABC in the third case.
Example 3: In this case, the very first and last selection of lines, as well as a line with certain patterns, are all deleted.
- You may use nd to remove the nth line.
- $d is used to remove the final line.
- You may use the syntax p, qd to remove a variety of lines from line no. p to q, as seen in the example below.
- Use /pattern/d to remove the first line containing a particular pattern, like in the previous example when the pattern is ABC.
This article gave us insight into how to edit a file using the Sed commands in Linux as a text stream editor SED. Choose whether to maintain the original file after making the necessary edits to the text and saving the more recent version. The sed command in Linux functions as an editor for text without an interactive user interface. Under the instructions we provide it to follow while it reads the text, it operates on piped text or input files. In Bash and other command-line shells, we may use the sed command to modify text in files and streams.