The sed
command, short for Stream Editor, is one of the most powerful and versatile tools available in the Linux terminal for text processing and manipulation. It allows users to perform basic text transformations on an input stream (a file or input from a pipeline). While it may seem complex at first, once you understand its syntax and capabilities, sed
becomes an indispensable tool for working with text files and automating tasks in scripts.
Basic Syntax of sed
The general syntax of the sed
command is as follows:
sed [options] 'command' file
options
: Flags that modify the behavior ofsed
.'command'
: The operationsed
will perform on the input data.file
: The file to be processed. If not provided,sed
will read from the standard input.
Simple Text Substitution with sed
The most common use of sed
is to perform text substitutions. You can replace a pattern (text or regular expression) with a new one using the s
command.
sed 's/old_text/new_text/' file.txt
This command searches for old_text
in file.txt
and replaces it with new_text
. By default, sed
only replaces the first occurrence of the pattern in each line.
For example, if you have a file with the following content:
The sky is blue.
The sun is yellow.
Running this sed
command:
sed 's/blue/red/' file.txt
Will output:
The sky is red.
The sun is yellow.
If you want to replace all occurrences of old_text
in each line, you can add the g
flag at the end:
sed 's/old_text/new_text/g' file.txt
This would replace every occurrence of old_text
in the file.
Using sed
with Regular Expressions
sed
also supports regular expressions (regex), which allows for more advanced text manipulation. For example, you can replace all digits in a file with an asterisk:
sed 's/[0-9]/*/g' file.txt
This command will replace each digit ([0-9]
) in the file with an asterisk. The g
flag ensures the substitution happens globally (on every occurrence in each line).
Deleting Lines with sed
The d
command in sed
allows you to delete lines from a file. For example, to delete the second line of a file, you can use:
sed '2d' file.txt
To delete lines that match a certain pattern, use a regular expression. For example, to remove all lines containing the word “sun”:
sed '/sun/d' file.txt
Inserting and Appending Lines with sed
sed
can also insert or append lines before or after a specific line in the file. To insert a line before a certain line number, use the i
command:
sed '2i This is a new line.' file.txt
This will insert the text “This is a new line.” before the second line.
To append text after a specific line, use the a
command:
sed '2a This is an appended line.' file.txt
This will append the text “This is an appended line.” after the second line.
Multiple Commands in One sed
Call
You can combine multiple sed
commands in a single execution using the -e
flag or by separating commands with semicolons:
sed -e 's/old/new/' -e 's/foo/bar/' file.txt
Alternatively, use semicolons:
sed 's/old/new/; s/foo/bar/' file.txt
This will apply both substitutions in sequence on each line.
Writing Changes to a File
By default, sed
does not modify the file in place. It simply outputs the result to the terminal. If you want to overwrite the file with the changes, you can use the -i
option (in-place editing):
sed -i 's/old_text/new_text/g' file.txt
Summary
While sed
may seem a bit intimidating due to its syntax, it is a powerful tool that can automate many text processing tasks on the Linux command line. Whether you’re replacing text, deleting lines, or making more complex transformations, sed
can save you a great deal of time and effort. By combining it with other tools like grep
, awk
, or find
, sed
becomes even more valuable in shell scripting and system administration.