How to Use the sed Command in Linux
The name sed stands for “Stream Editor,” and it’s a powerful utility that allows you to parse and transform text right from the command line. Whether you’re dealing with configuration files, scripts, or even plain text, sed
is your go-to tool for quick and efficient text manipulation.
The primary use of sed
is to search for specific patterns of text and replace them with something else. It can also delete or insert lines and perform other text transformations. It’s particularly useful for batch editing of files or for working within shell scripts to automate various tasks.
While sed is incredibly versatile on its own, it’s often used in combination with other Linux commands like awk
for text processing, grep
for pattern searching, and cat
for displaying file content. Together, these tools form a robust toolkit for text processing in the Linux environment.
General syntax for sed
command:
$ sed [OPTIONS] [FILE]...
1. Text substitution
echo "Text" | sed 's/Replaceable_Word/The_Word_That_Replaces/'
Use the sed
command to search and replace any part of the text. 's'
indicates a search and replace task.
Example:
Let’s say you have a string “I love CSS
” and you want to replace “CSS” with “CSS Libraries
“.
echo "I love CSS" | sed 's/CSS/CSS Libraries/'
I love CSS Libraries
In this example, the echo command outputs “I love CSS
“, and then sed replaces “CSS
” with “CSS Libraries
“. The final output is “I love CSS Libraries
“.
2. Replace text on a specific line in a file
sed '[line] s/harder/easier/g' [file]
The 'g'
option of the sed
command is used to replace anything that matches the pattern.
Example:
Let’s say you have a text file named example.txt
with the following content:
Life is hard. Working harder is the key to success. The harder you work, the luckier you get.
To replace all occurrences of the word “harder
” with “easier
” on line 2 of example.txt
, you would run:
sed '2 s/harder/easier/g' example.txt
After running the command, the output displayed on the terminal would be:
Life is hard. Working easier is the key to success. The harder you work, the luckier you get.
Note that the word “harder
” is replaced with “easier
” only on line 2.
If you want to save these changes back to the file, you can use the -i
option:
sed -i '2 s/harder/easier/g' example.txt
After running this command, the content of example.txt
will be permanently changed to:
Life is hard. Working easier is the key to success. The harder you work, the luckier you get.
3. Replace first matching with new text
sed 's/harder/easier/' [file]
This command replaces only the first match of the search pattern.
Example:
Let’s say you have a text file named example.txt
with the following content:
Life is harder than we think. Working harder is the key to success. No pain, no gain. Work harder.
You can use the sed command to replace the word “harder
” with “easier
” in each line:
sed 's/harder/easier/' example.txt
After running the command, the output will be:
Life is easier than we think. Working easier is the key to success. No pain, no gain. Work easier.
4. Remove matching lines
sed '/Something/d' example.txt
Use the d
option of the sed
command to remove any line from a file.
Example:
Let’s say you have a file called example.txt
with the following content:
Hello World Something is here Another line Yet another line Something else
Running the command sed '/Something/d' example.txt
will output:
Hello World Another line Yet another line
5. Search for a case-insensitive word + delete it
sed '/Sample/Id' example.txt
The I
option of the sed
command is used to search for a matching pattern in a case insensitive way.
Example:
Let’s say you have a file named example.txt
with the following content:
This is a Sample line. Another line. Yet another Sample line. Final line.
Running the command sed '/Sample/Id' example.txt
will produce the following output:
Another line. Final line.
6. Replace words with uppercase
sed 's/\(libraries\)/\U\1/Ig' example.txt
Use the U
option of the sed
command to convert any text to uppercase letters.
Example:
Let’s say you have a file named example.txt
with the following content:
I love libraries. libraries are great. You can find many books in libraries.
After running the sed
command, the output will be:
I love LIBRARIES. LIBRARIES are great. You can find many books in LIBRARIES.
7. Replace words with lowercase
sed 's/\(libraries\)/\L\1/Ig' example.txt
The L
option of the sed
command is used to convert any text to lowercase letters.
Example:
Let’s say you have a file named example.txt
with the following content:
Libraries are essential for research. libraries help in many ways. I love LIBRARIES!
After running the sed
command, the output will be:
libraries are essential for research. libraries help in many ways. I love libraries!
8. Insert blank lines in a file
sed G [file]
Use the G
option of the sed
command to insert blank lines after each line of the file.
Example:
Let’s say you have a file called example.txt
with the following content:
Hello World This Is A Test
You can run the following command to append an extra newline at the end of each line:
sed G example.txt
After running the command, the output will be:
Hello World This Is A Test
9. Print file’s line numbers
sed '=' [file]
The =
sign is used to print a line number before each line of text in a file.
Example:
Let’s say you have a file named example.txt
with the following content:
Hello World This Is A Test
You can run the following command to print the line numbers before each line:
sed '=' example.txt
1 Hello 2 World 3 This 4 Is 5 A 6 Test
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |