Using the SED Command in Linux

Using the SED Command in Linux

4 mins read323 Views Comment
Updated on Feb 25, 2023 18:05 IST

Discover the versatility of the sed command in Linux for text manipulation in files and streams. Our guide covers basic and advanced features to enhance your productivity.

2023_02_Sed-command-in-Linux.jpg

The sed command in Linux (short for “stream editor”) is a powerful tool for manipulating text in a file or stream. It allows you to perform a wide range of text transformations, such as replacing text, deleting lines, appending text to a line, and more. This article on the sed command in Linux focuses on applying the STDIN library, Global replacement, removing a line, limiting the SED, transforming the characters, etc.

What SED Command Is

It is a capable text stream editor is SED. able to insert, remove, search for, and replace (substitution).

The sed command in Linux substitutes a text. It looks through a file for the given pattern and replaces it with the desired string. You can also use the substitute commands and delimiters (mostly slashes – /) for replacing the text using the sed command.

Regular expression support in the SED command of Unix enables it to do intricate pattern matching.

Syntax


 
sed OPTIONS... [SCRIPT] [INPUTFILE...]
Copy code

Options

The sed command has the following command-line options:

  • -n, –quiet, –silent: It completely enables us to print in the pattern space.
  • -e script, –expression=script: It is for including the script in the list of commands to run.
  • -f script-file, –file=script-file: It is for including the script-contents files in the list of commands to be executed.
  • –follow-symlinks: When processing in situ, one follows symlinks.
  • -I [SUFFIX], –in-place[=SUFFIX]: It alters existing files (creates backup if SUFFIX option is supplied).
  • -l N, –line-length=N: For the ‘l’ command, it indicates the preferred line-wrap length.
  • –posix:  It turns off all GNU extensions.
  • -E, -r, –regexp-extended: We can use the expanded regular expressions in the script because of it (for portability use POSIX -E).
  • -s, –separate: It extends the lengthy stream by treating files as individual entities rather than as a single unit.
  • –sandbox: It runs in sandbox mode.
  • -u, –unbuffered: It loads the least amount of data possible from the input files and flushes the output buffers more frequently.
  • -z, –null-data: Lines are divided using NUL letters.
  • –help: The help manual shows on it.
  • —version: Display information about the version using it.

Applying to the STDIN directory

We can use the sed tool to manipulate the STDIN directory in addition to files.


 
echo class7 | sed 's/class/jtp/'
echo class7 | sed 's/7/10/'
cat msg.txt | sed 's/learn/study/'
Copy code

Global Replacement

In the preceding instance, not all words beginning with “learn” were changed to “study.” We must employ a worldwide replacement letter, “g,” to alter each word. Any words you specify in a file or string will edit itself.

Syntax


 
command | sed 's/<oldWord>/<newWord>/g'
Copy code

Removing a line

We can delete a whole line from a file by using the ‘d’ option. The line will be erased if just one word from that line is specified with the ‘d’ option. However, be aware that all lines containing that word will be removed. So, it will be as in the syntax below.

Syntax


 
cat <Name_of_the_file> | sed '/<Word>/d'
Copy code

Using the multiple SED commands:

Using the ‘-e’ option, we can run several sed commands simultaneously. So, by running the command, we can run sed many times.


 
sed -e '<script 1> ; <script 2>' <Name_of_the_file>
Copy code

Reading Commands from a File

You can save the sed commands in a file and instantly execute in any file. The following syntax can be used to specify the ‘-f’ option:


 
sed -f <sed file> <file name>
Copy code

Replacing characters

The find and replace command of sed is the s command. It instructs sed to search for every instance of “old-text” and replace it with “new-text” in the input.txt file.


 
sed -i 's/old-text/new-text/g' input.txt
Copy code

Limit the SED

The ‘sed’ command’s default use involves processing the whole file. By specifying any line, you can restrict it. And, you can do it in two ways:

  1. A selection of lines.
  2. A pattern that coincides with a certain line.

We can provide the following number to identify a line:


 
sed '3s/Red/Blue/' exm.txt
Copy code

Inserting and Appending Text

To insert and add text to a file, use the I and “a” flags. The text before the string comes to add using the I flag, while the text appearing after the string comes to add using the ‘a’ flag. 

Comment


 
echo "Another Demo" | sed 'iFirst Demo'
Copy code

Modifying Lines

Use the “c” flag to change a particular line. Use the following command to change a line:


 
sed '3cThis is a modified line.' exm.txt
Copy code

Transformation of Characters

Change characters using the ‘y’ flag. So note, you cannot restrict character evolution to particular instances. Run the command as follows to change characters:


 
sed 'y/abc/def/' exm.txt
Copy code

Printing the Line Numbers

The line number is printed using the symbol “=”.

Run the following command to output the line number:

Syntax


 
sed '=' exm.txt
Copy code

Conclusion

The blog explains the syntax and code snippets of sed command in Linux. While the sed command can be complex and take some time to master, it can be a valuable tool for Linux users who regularly need to perform text manipulation tasks. And its many commands and options can help you streamline your text-processing workflows and save time and effort.

Explore all the Linux courses to learn more about commands.

Contributed By Megha Chadha

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio