sed Command in Linux



sed (Stream Editor) is a powerful command-line tool used on Unix-based systems for text processing. It allows users to search, replace, insert, and delete lines in a file without opening it in a traditional text editor. This feature makes sed an efficient choice for automating text modifications in scripts or processing large files.

The sed command enables you to apply changes directly to data streams, making it useful for log file analysis, data transformation, and system administration tasks.

Table of Contents

Here is a comprehensive guide to the options available with the sed command −

sed Command Options

The general format for using the sed command in Linux is shown below −

sed [OPTIONS] [SCRIPT] [INPUT_FILE...]

Here, the script defines the editing instructions, and the input file contains the text to be processed.

The sed command in Linux comes with a variety of options to customize its behavior. Here are some of the key options −

OptionDescription
-b, --binaryOpens input files in binary mode, treating line endings as line feeds.
--debugEnables debug mode, printing input in its canonical form and providing details about program execution.
--follow-symlinksAllows editing of the target file if the specified file is a symbolic link. This option must be used with the -i option.
--helpDisplays usage information for the sed command.
-i, --in-place [=SUFFIX]Edits the file in place, overwriting the original file.
--posixDisables any extensions to the standard POSIX sed, ensuring the script remains portable.
--versionShows the current version of sed installed on your system.
-E, -r, --regexp-extendedUses extended regular expressions, which allow for more complex pattern matching.
-e script, --expression=scriptAllows you to specify a script of commands to run with sed.
-f script-fileExecutes commands from a script file instead of providing them inline.
-l N, --line-length=NSets the maximum line length for the l command (default is 70 characters).
-n, --quiet, --silentSuppresses the output, so sed does not print anything unless explicitly instructed.
-s, --separateTreats each specified file as a separate entity rather than combining them into a continuous stream.
--sandboxDisables running external programs, restricting the operation to the input files specified on the command line.
-u, --unbufferedReduces buffering for input and output, processing data immediately.
-z, --null-data, --zero-terminatedTreats the input as a series of lines, where each line is terminated by a null byte instead of a newline.

For more details, you can access the sed’s manual page −

man sed
sed Command in Linux1

How to Use sed Command in Linux?

Let's create a sample file and provide some content in it −

nano tutorialspoint.txt
sed Command in Linux2

Now go through the following examples to see how the sed command works in Linux −

How to Replace a String with sed

We can use the substitute command s along with delimiters, usually slashes (/), to replace text using sed.

sed 's/old_string/new_string/' tutorialspoint.txt

Replace old_string with the text you want to change, and new_string with the replacement text. For example, you can use the following command to replace every instance of "tutorialspoint" with "linuxtutorials" in a file named tutorialspoint.txt −

sed 's/tutorialspoint/linuxtutorials/' tutorialspoint.txt
sed Command in Linux3

Keep in mind that sed doesn’t modify the original file unless you specifically tell it to. By default, it only shows the changes in the output.

If you want to overwrite the original file with the changes, use the -i option. However, it's a good idea to test the command first before making permanent changes.

Alternatively, you can save the changes to a new file by redirecting the output. Simply add > newfilename.txt at the end of the command.

Replacing All Occurrences of String

By default, the sed command only replaces the first occurrence of a specified string in each line. If the string appears multiple times in a line, only the first instance will be modified.

For example, the following command replaces the first occurrence of "linux" with "python" in each line −

sed 's/tutorials/guides/' tutorialspoint.txt

The output shows that only the first occurrence is replaced with the specified string −

sed Command in Linux4

To replace all instances of a string within a line, we need to use the g flag −

sed 's/tutorials/guides/g' tutorialspoint.txt

This time, the sed command replaces every instance of "tutorials" with "guides" throughout the file.

sed Command in Linux5

Replacing a Specific Occurrence in a Line

You can specify which occurrence of a string to replace within each line by using a number flag (e.g., 1, 2) to indicate the desired occurrence. For example, to replace the second instance of "tutorials" with "guides", use the following command −

sed 's/tutorials/guides/2' tutorialspoint.txt

This time, the sed command replaces only the second occurrence of the word "tutorials" in each line −

sed Command in Linux6

Print Only Lines with Substituted Text

By default, sed displays the entire file after performing substitutions. To display only the lines where a substitution has been made, use the -n option along with the p command −

sed -n 's/linux/python/p' tutorialspoint.txt

This time, sed returns only that line in which the string linux is replaced with python −

sed Command in Linux7

Replacing String in a Specific Line

To substitute a string in a particular line, prefix the s command with the line number. For instance, to replace "tutorials" with "guides" only in the third line, use the following command −

sed '3 s/tutorials/guides/' tutorialspoint.txt
sed Command in Linux8

Similarly, to replace a string across a specific range of lines, specify the range in this format −

sed '2,4 s/tutorials/guides/' tutorialspoint.txt

This command will substitute tutorials with guides in lines 2, 3, and 4 −

sed Command in Linux9

Deleting a Specific Line

To remove a particular line from a file, use the d command along with the line number. For example, to delete the third line, use the following command −

sed '3d' tutorialspoint.txt

The third line has been dropped from the file −

sed Command in Linux10

Similarly, you can use the other available options to perform specific text-processing tasks.

Conclusion

The sed command is an essential tool for text processing in Unix-based systems. It allows users to efficiently edit files directly from the command line. Its versatility enables users to search, replace, delete, and insert text, which makes it ideal for automating text manipulation in scripts and handling large files.

Whether you're replacing specific occurrences of a string, modifying lines within a range, or deleting lines altogether, sed offers a wide array of options to meet various needs.