What is the SED command in GNU/Linux and example?

In GNU/Linux A sed command stands for Stream Editor, and It was developed in 1974 by the Lee E McMahon.

A sed command had a different version for macOS sed command is managed by Berkley Software Distribution BSD, and for GNU/Linux SED is managed by General Public License GNU.

It is highly used command to manipulate TextFile to achieve task like searching, find and replace, insertion or deletion.

A sed command has two buffer such as

  • sed pattern buffer
  • sed hold buffer

When you pass the sed command on terminal, it read the input text file one-by-one and matches pattern and stores data into Pattern buffer, It like temporary storage, and it moves ahead to search for another pattern.

In the Hold buffer, It stores all the pattern buffer for later use, and you cannot apply any operation on Hold Buffer Data.

A combining of both pattern and hold buffer help you to find the next matching pattern and process the result.

Install sed command

In the GNU/Linux environment, sed command utility is pre-install So, you do not need to install, but for macOS, the user has BSD version.

If macOS user wants to use GNU/Linux Environment, they can install using HomeBrew.

To install sed command on macOS user need to type command

brew install gnu-sed

How to use basic sed command

In sed command can read the value from the standard input (STDIN) or the input text file.

We will show you the basic usage of sed command with the example so, you will get abetter understanding.

To Read Input File

If you want to read any text file instead of using cat command, use the sed command to read.

To read text file you can use the following command

$ sed '' filename
Demo 1
Demo 2
Demo 3
Demo 4
Demo 5

When you pass this command in single quote ” with the input source, it will read and print the output to the terminal screen.

Duplicate Lines

If you want a print or duplicate line multiple time from the Input file, you can use the parameter p inside the single quotes.

To Duplicate line copy paste or type the following command

$ sed 'p' filename
Demo 1
Demo 1
Demo 2
Demo 2
Demo 3
Demo 3
Demo 4
Demo 4
Demo 5
Demo 5

By default, when you run the sed command, it read and print the file content, but we added parameter p to print line multiple times.

If you don’t want to print line multiple time, then pass the parameter -n along with 'p' to stop multiple printing.

Substituting Text

This is an important part of the sed command, and It helps you to search a specific pattern or regular expression from the input file and replace the matched with the provided strings or data.

Command Syntax

$ sed 's/search-file/replace-file' filename

Example Command

$ sed 's/Demo/number/' filename
number 1
number 2
number 3
number 4
number 5

This command will search for the “Demo” in the input file and replace with the string “number”.

That’s it for basic information about sed command. In the upcoming articles, we will share you the sed command usage every day.

If you want to share something with us,please comment down below.

Leave a Reply