navigation

What is Sed? Learn about the Sed command in Linux

Sed is a line editor, commonly used to change text in files on the Linux operating system.

Picture 1 of What is Sed? Learn about the Sed command in Linux

Sed is an extremely useful command line utility for editing text, searching and replacing strings, deleting lines, inserting lines and many other operations. This article from  TipsMake  will give you an overview of what is Sed? basic usage of Sed.

What is the Sed command?

Sed is a stream editor, commonly used to change text in files. It stands for 'stream editor'. Sed reads each line of text from the input, makes the specified changes, and then outputs the results. The changes are identified using regular expressions (regex). This makes it a highly efficient tool for managing and editing text, especially when working with large files or automating tasks in commands.

Syntax of Sed command in Linux

The basic syntax of the sed command is as follows:

sed [options] 'command' file

In there:

- options are options to customize sed's behavior, such as -n to disable the default line printing mode or -i to directly change the input file.

- command is one or more commands to be executed for each line of the file.

Commonly used options in Sed command

sed [OPTIONS]. {script-only-if-no-other-script} [input-file].

Options:

Here are some command line options of the sed command:

-n, --quiet, --silent: Enable forced printing of pattern space.

–help: print command usage information.

-f script-file, --file=script-file: Used to append the contents of the script-file to the commands to be executed.

--follow-symlinks: Follow symbolic links when processing in-place.

–posix: Disable the POSIX sed extension to simplify writing portable scripts.

-l N, --line-length=N: Used to specify the desired line break length for the `l' command.

-E, -r, --regexp-extended: Allows us to use extended regular expressions in the script (for ease of conversion, use POSIX -E).

-i[SUFFIX], --in-place[=SUFFIX]: Used to edit files in place.

-s, --separate: used to treat the files as separate instead of as a single file and continue the long stream.

--sandbox: Disable external programs and only operate on input files on the command line.

-u, --unbuffered: Used to load minimal amount of data from input files and flush output buffers more frequently.

-z, --null-data: Used to separate lines with the NUL character.

--help: used to display help instructions.

--version: Used to display version information.

Picture 2 of What is Sed? Learn about the Sed command in Linux

What is the Sed command?

Detailed instructions on how to use the Sed command

Some systems have the sed command built into the machine, but some do not. Therefore, to know if the machine can use the sed command or not, you need to check with the command: sed --version.

If not, please install according to your operating system using the following commands:

- Ubuntu/Debian: sudo apt-get install sed

- CentOS/RHEL: sudo yum install sed

- macOS (with Homebrew): brew install gnu-sed

Once installed, you can start using the sed commands presented in the next section of the tutorial.

Conclude

The sed command is an indispensable tool in the toolkit of any Linux system administrator, as well as anyone who works regularly with the command line. From simple operations to complex ones, sed allows you to process text efficiently and flexibly.

Through this article of TipsMake, hopefully you have grasped the basic knowledge about sed and can confidently apply it to your work.

Update 12 February 2025