en
  English
Phone :  +370 (5) 204-1903
Email :  sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. SED command in Linux - [replace with sed tutorial]

SED command in Linux - [replace with sed tutorial]

The sed command refers to the stream editor found in UNIX-based operating systems. Primarily, this command edits text coming in from a file or standard input. It performs many functions, including finding and replacing, deletion, searching, and insertion. The most common uses for the sed command are: find and replace or substitution. When using sed, users can edit files without needing to open them. 

Linux Tutorial May 11, 22 by Nisal N 7 min Read
SED command in Linux - [replace with sed tutorial]


Using SED Linux is a considerably faster process of finding and replacing things in the file instead of first opening a file on VI Editor and then proceeding to make alterations. All editing operations decisions occur when calling on the command. That is a powerful approach to text transformation. In summary, sed is a potent text stream editor that supports regular expression allowing it to carry out complicated pattern matching. 

How to edit text streams using SED Linux

Because the sed command works on text files or input streams, you can send the output of various other commands directly to it. This command outputs everything on STDOUT (standard output) by default. If you wish to save the result within a file, you can do so by redirecting the output from the sed command. 

Syntax 

The syntax for the SED command in Linux:

sed [options] commands [input file] 

SED Examples that you can use (SED tutorial)

The SED command in UNIX has a wide variety of functions and uses. As previously mentioned, some of these functions are printing, deleting, replacing, and saving text in a file. Because of the various roles, different commands need to be executed to perform these functions. Below are samples of some common sed commands. 

1.   Printing a file 

The sed command transfers its output to the standard one by default. Therefore, you can view the file's contents without passing any editing commands by it. In this command, you are supposed to leave the commands field blank, as illustrated below: 

sed" example.txt 

Another way to print the contents of a particular file is by offering the standard input instead of the file:

cat example.txt | sed"

2.   Substituting or replacing SED string of text 

The sed command is commonly used to replace or substitute text within a file. It can utilize regular expressions in searching for text patterns and follow this up with replacing found text with the preferred value. You can specify the line numbers range to the command for this procedure.

Below is a simple sed replace command that is used to replace "Unix" with "Linux" within a particular field:

$sed 's/unix/linux/' file.txt 

In the above command, the "s" specifies that particular substitution operation. The "/" is the delimiters and "unix" is the pattern, while "linux" represents the replacement string. $ defines the file's last line.

Sed substitutes the occurrence of each line's first pattern, and it will not do this for the subsequent second, third, or fourth occurrence in this line.

3.   Substituting a string on a particular line number 

Users can restrict sed so as to substitute the string on a particular line number. Below is an example of this type of command. 

$sed '3 s/unix/linux/' file.t

The above example only replaces the line string on the third line. 

4.   Deleting lines for a specific file 

A sed command is used to delete specific lines from a field. This command performs the deletion operation even without opening the file. For instance, if you want to delete a particular line from a Syntax, you should follow this command: 

$ sed 'cd' filename.txt

5.   Substituting all occurrences of the pattern within a line

"/g" is the substitute flag tasked with specifying the sed command for replacing all occurrences of strings on a line. It stands for global replacement. Here is a sample command used to perform this function:

$sed 's/unix/linux/2' file.txt 

6.   Parenthesizing each word's first character 

The following example enables users to print each word's first character in parenthesis:  

sed 's/\(\b[A-Z]\)/\(\1\)/g' 

7.   Substituting nth occurrence to all occurrences within a line 

You are supposed to use the combinations "/1, /2..." and "/g" to replace all patterns from the nth occurrence within a line. The sed command below demonstrates how to replace the third, fourth, fifth, sixth..." "Unix" word with "Linux" within a line:

$sed 's/unix/linux/3g' file.txt

This particular command will only replace the string on the third line. 

8.   Printing the replaced lines only 

If you want to display only replaced lines, you should use the "-n" option and the "/p" print flag. In this sample, the "-n" option is used to suppress the duplicate rows generated by the "/p" flag. It prints the substituted lines only once:

$sed -n 's/unix/linux/p' file.txt 

If you choose to use "-n" without "/p," the sed command will not print anything. 

9.   Duplicating a replaced line using the /p flag

The "/p" flag prints a replaced line just twice on the terminal. If the line doesn't have a search pattern and isn't replaced, then the "/p" flag only prints it once.

$sed 's/unix/linux/p' file.txt 

Sed command vs grep command 

The grep command tends to come up alongside the SED UNIX command when discussing basic text search or manipulation. They are similar in some ways, although they are indeed different commands. 

The grep command is a line-based search function mainly used to return lines from a single file or multiple files. These lines are matched with specific search terms. Sed is similar to the former because it is also a line-by-line tool. However, it is primarily intended for string replacement in lines (sed replace string), unlike its counterpart.

Furthermore, grep focuses on each line of text within a file and looks for a match against a provided term. The first basic syntax in this command is a matcher followed by the search space. The sed command substitutes the occurrence of a particular set of characters with a different one within a specific field. 

Take away 

Many power users enjoy using the sed replace command for its replacement abilities. Therefore, they can carry out text manipulation functions more efficiently and faster. The samples provided above in the SED tutorial provide a blueprint on where to start if you are a beginner that is not very familiar with the sed command. 

People also read: 

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment