Table of Contents
In this article, we will show you how to use sed command in Linux and explain its basic syntax and options. We will also demonstrate some common and useful examples of sed command in action. By the end of this article, you will have a good understanding of how to use sed command in Linux and what it can do for you.
Before we start, you need to make sure that you have sed command installed on your Linux system. Sed command is usually pre-installed on most Linux distributions, but if you don’t have it, you can install it using your package manager. For example, on Debian or Ubuntu, you can use the following command to install sed:
sudo apt install sed
Basic Syntax and Options of Sed Command
The basic syntax of sed command is as follows:
sed [options] 'command' filename
Here, options
are the optional flags that modify the behavior of sed command, command
is the instruction that tells sed what to do with the input, and file
is the name of the file that provides the input. If no file is specified, sed will read from the standard input.
Some of the common options of sed command are:
-e
: Allows multiple commands to be executed in a single invocation of sed.-f
: Reads commands from a file instead of the command line.-i
: Edits the file in-place instead of printing the output to the standard output.-n
: Suppresses the automatic printing of the pattern space (the buffer that holds the current line).-r
: Enables extended regular expressions, which provide more features and flexibility than basic regular expressions.
Sed commands are usually enclosed in single quotes to prevent shell expansion and interpretation. The most common sed command is s
, which stands for substitute. The syntax of the substitute command is:
s/pattern/replacement/flags
Here, pattern
is a regular expression that matches the text to be replaced, replacement
is the text that replaces the matched text, and flags
are optional modifiers that affect how the substitution is performed.
Some of the common flags for the substitute command are:
g
: Replaces all occurrences of the pattern in a line instead of just the first one.p
: Prints the original line before performing the substitution.w file
: Writes the substituted line to a file instead of printing it to the standard output.n
: Replaces only the nth occurrence of the pattern in a line.
Substituting Text with Sed Command
One of the most common uses of sed command is to replace text in a file or a stream. You can use the substitute command with different options and flags to achieve various results. Here are some examples of how to use sed command to substitute text:
Replace first Occurrence
To replace the first occurrence of foo
with bar
in a file named test.txt
, you can use:
sed 's/foo/bar/' test.txt
Replace all Occurrences
To replace all occurrences of foo
with bar
in a file named test.txt
, you can use:
sed 's/foo/bar/g' test.txt
Replace Specific Occurrences
To replace only the third occurrence of foo
with bar
in a file named test.txt
, you can use:
sed 's/foo/bar/3' test.txt
To replace from the second occurrence to all occurrences of foo
with bar
in a file named test.txt
, you can use:
sed 's/foo/bar/2g' test.txt
To use case-insensitive matching and replace all occurrences of foo
with bar
in a file named test.txt
, you can use:
sed 's/foo/bar/gI' test.txt
To use backreferences and replace all occurrences of <tag>
with <tag></tag>
in a file named test.txt
, you can use:
sed 's/<\(.*\)>/<\1><\/\1>/g' test.txt
Here, \(...\)
creates a group that captures the text inside it, and \1
refers to the first captured group.
Selecting Text with Sed Command
Another common use of sed command is to select and print text from a file or a stream. You can use the print command, which is p
, to print the pattern space. You can also use the address range, which is /pattern1/,/pattern2/
, to specify which lines to print. Here are some examples of how to use sed command to select text:
Print Specific Lines
To print only the 10th line of a file named test.txt
, you can use:
sed -n '10p' test.txt
Here, -n
option suppresses the automatic printing of the pattern space, and 10p
prints only the 10th line.
To print only the lines from 5 to 15 of a file named test.txt
, you can use:
sed -n '5,15p' test.txt
Here, 5,15
specifies the range of lines to print.
Print Lines that Match a Specific Pattern
To print only the lines that match the pattern foo
in a file named test.txt
, you can use:
sed -n '/foo/p' test.txt
Here, /foo/
matches the lines that contain foo
.
To print only the lines that do not match the pattern foo
in a file named test.txt
, you can use:
sed -n '/foo/!p' test.txt
Here, /foo/!
matches the lines that do not contain foo
.
To print only the lines that contain the word foo
in a file named test.txt
, you can use:
sed -n '/\<foo\>/p' test.txt
Here, \<
and \>
match the word boundaries.
Inserting Text with Sed Command
You can also use sed command to insert text before or after a line or a pattern in a file or a stream. You can use the insert command, which is i
, to insert text before a line, and the append command, which is a
, to insert text after a line. Here are some examples of how to use sed command to insert text:
Insert Text before First Line
To insert the text Hello World
before the first line of a file named test.txt
, you can use:
sed '1iHello World' test.txt
Here, 1
specifies the first line and the i
option specifies the insert action.
Insert Text after Last Line
To insert the text Hello World
after the last line of a file named test.txt
, you can use:
sed '$aHello World' test.txt
Here, $
specifies the last line.
Insert Text before Every Line
To insert the text Hello World
before every line that matches the pattern foo
in a file named test.txt
, you can use:
sed '/foo/iHello World' test.txt
Here, /foo/
matches the lines that contain foo
.
Insert Text after Every Line
To insert the text Hello World
after every line that matches the pattern foo
in a file named test.txt
, you can use:
sed '/foo/aHello World' test.txt
Here, /foo/
matches the lines that contain foo
.
Insert Text at Beginning of Every Line
To insert the text Hello World
at the beginning of every line in a file named test.txt
, you can use:
sed 's/^/Hello World/' test.txt
Here, ^
matches the beginning of a line.
Insert Text at Ending of Every Line
To insert the text Hello World
at the end of every line in a file named test.txt
, you can use:
sed 's/$/Hello World/' test.txt
Here, $
matches the end of a line.
Deleting Text with Sed Command
You can also use sed command to delete text from a file or a stream. You can use the delete command, which is d
, to delete lines or patterns from the input. Here are some examples of how to use sed command to delete text:
Delete First Line
To delete the first line of a file named test.txt
, you can use:
sed '1d' test.txt
Here, 1
specifies the first line and d
option specifies the delete action.
Delete Last Line
To delete the last line of a file named test.txt
, you can use:
sed '$d' test.txt
Here, $
specifies the last line.
Delete Lines with Pattern
To delete all the lines that match the pattern foo
in a file named test.txt
, you can use:
sed '/foo/d' test.txt
Here, /foo/
matches the lines that contain foo
.
To delete all the lines that do not match the pattern foo
in a file named test.txt
, you can use:
sed '/foo/!d' test.txt
Here, /foo/!
matches the lines that do not contain foo
.
To delete all the blank lines in a file named test.txt
, you can use:
sed '/^$/d' test.txt
Here, ^$
matches the lines that are empty.
To delete all the lines that start or end with whitespace in a file named test.txt
, you can use:
sed '/^[[:space:]]\|[:space:]]$/d' test.txt
Here, [[:space:]]
matches any whitespace character, and \|
is the logical OR operator.
Other Useful Operations with Sed Command
Besides substituting, selecting, inserting, and deleting text, sed command can also perform other useful operations on files or streams. Here are some examples of how to use sed command to do other things:
To append the text Hello World
to every line that matches the pattern foo
in a file named test.txt
, you can use:
sed '/foo/s/$/Hello World/' test.txt
Here, /foo/
matches the lines that contain foo
, and s/$/Hello World/
appends the text to the end of the line.
To change the text foo
to bar
only in the lines that match the pattern baz
in a file named test.txt
, you can use:
sed '/baz/s/foo/bar/' test.txt
Here, /baz/
matches the lines that contain baz
, and s/foo/bar/
replaces the text in those lines.
To transform all the lowercase letters to uppercase letters in a file named test.txt
, you can use:
sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' test.txt
Here, y
is the transform command, and it takes two sets of characters as arguments. It replaces each character in the first set with the corresponding character in the second set.
To execute a shell command on every line that matches the pattern foo
in a file named test.txt
, you can use:
sed '/foo/e date' test.txt
Here, e
is the execute command, and it takes a shell command as an argument. It executes the command on every matched line and prints the output.
Examples of Sed Command in Action
To give you a better idea of how to use sed command in Linux, here are some examples of common tasks and how to achieve them with sed command:
Task | Sed Command |
---|---|
Replace all occurrences of http with https in a file named urls.txt | sed 's/http/https/g' urls.txt |
Delete all comments (lines starting with #) in a file named config.txt | sed '/^#/d' config.txt |
Insert a line number at the beginning of every line in a file named test.txt | sed '=' test.txt | sed 'N;s/\n/\t/' |
Reverse the order of words in every line in a file named test.txt | sed 's/\([[:alnum:]]*\) *\([[:alnum:]]*\)/\2 \1/' test.txt |
Remove all HTML tags from a file named webpage.html | sed 's/<[^>]*>//g' webpage.html |
Here are some tips and tricks for using sed command effectively:
You can use multiple commands with sed by separating them with semicolons or using -e
option. For example, to delete all blank lines and comments from a file named config.txt
, you can use:
sed '/^$/d;/^#/d' config.txt
or
sed -e '/^$/d' -e '/^#/d' config.txt
You can use multiple files as input for sed by specifying them after the command. For example, to replace all occurrences of foo
with bar
in two files named file1.txt
and file2.txt
, you can use:
sed 's/foo/bar/g' file1.txt file2.txt
You can use standard input as input for sed by using -
as the file name. For example, to replace all occurrences of foo
with bar
in the output of another command, you can use:
echo "This is foo" \| sed 's/foo/bar/' -
You can use standard output as output for sed by omitting the file name. For example, to print only the lines that match the pattern foo
from a file named test.txt
, you can use:
sed -n '/foo/p' test.txt
You can use variables or shell commands as arguments for sed by using double quotes instead of single quotes. For example, to replace the current date with the word today
in a file named test.txt
, you can use:
sed "s/$(date)/today/" test.txt
Conclusion
In this article, we have learned how to use sed command in Linux and what it can do for us. We have seen the basic syntax and options of sed command, and how to use it to perform various text processing tasks such as substituting, selecting, inserting, deleting, and transforming text. We have also seen some examples of sed command in action and some tips and tricks for using it effectively.
Thank you for reading this article. We hope you have enjoyed it and learned something new.