Table of Contents
Sed stands for 'Stream Editor' and it is a powerful utility that allows you to parse and convert text right from the command line. Whether you are dealing with configuration files, scripts, or even plain text, sed is the tool that helps you manipulate text quickly and efficiently.
Key Takeaways About Use Sed Command in Linux
- Sed stands for 'Stream Editor' and it is a powerful utility that allows you to parse and convert text right from the command line.
- Whether you are dealing with configuration files, scripts, or even plain text, sed is the tool that helps you manipulate text quickly and efficiently.
- The main use of sed is to search for specific patterns of text and replace them with something else.
The main 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 conversions. It is especially useful for batch editing files or working in shell scripts to automate various tasks.
Although sed itself is extremely versatile, it is often used in combination with other Linux commands such as awk for text processing, grep for pattern searching, and cat for displaying file contents. Together, these tools form a powerful toolkit for text processing in a Linux environment.
General syntax for sed command:
$ sed [OPTIONS] [FILE].
1. Replace Text
echo "Text" | sed 's/Replaceable_Word/The_Word_That_Replaces/'
Use the sed command to search and replace any part of text. 's' represents a search and replace action.
For example, let's say you have the 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' , then sed replaces 'CSS' with 'CSS Libraries' . The end result is 'I love CSS Libraries' .
2. Replace Text on a Specific Line in the File
sed '[line] s/harder/easier/g' [file]
The 'g' option of the sed command is used to replace anything that matches the pattern.
For example, suppose 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 result displayed on the terminal will 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 by 'easier' only in line 2.
If you want to save these changes to a file, you can use the -i option :
sed -i '2 s/harder/easier/g' example.txt
After running this command, the contents 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 the First Match with New Text
sed 's/harder/easier/' [file]
This command replaces only the first match of the search pattern.
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 result will be:
Life is easier than we think. Working easier is the key to success. No pain, no gain. Work easier.
4. Delete Matching Lines
sed '/Something/d' example.txt
Use the d option of the sed command to delete any line from the file.
For example, suppose you have a file named 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.txtwill output:
Hello World Another line Yet another line
5. Search for 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 manner.
For example, suppose you have a file named exampleb.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.txtwill produce the following output:
Another line. Final line.
6. Replace Words with Capital Letters
sed 's/(libraries)/U1/Ig' example.txt
Use the U option of the sed command to convert any text to uppercase.
I love libraries. libraries are great. You can find many books in libraries.
After running the sed command, the result will be:
I love LIBRARIES. LIBRARIES are great. You can find many books in LIBRARIES.
7. Replace Words with Lowercase Letters
sed 's/(libraries)/L1/Ig' example.txt
The L option of the sed command is used to convert any text to lowercase.
Libraries are essential for research. libraries help in many ways. I love LIBRARIES!
libraries are essential for research. libraries help in many ways. I love libraries!
8. Insert Blank Lines into the File
sed G [file]
Use the G option of the sed command to insert a blank line after each line of the file.
Hello World This Is A Test
You can run the following command to add a new line to the end of each line:
sed G example.txt
Hello World This Is A Test
9. Print the Line Number of the File
sed '=' [file]
The = sign is used to print the line number before each line of text in a file.
Hello World This Is A Test
You can run the following command to print the line number before each line:
sed '=' example.txt
1 Hello 2 World 3 This 4 Is 5 A 6 Test
FAQ
How do you use sed command in Linux?
Sed stands for 'Stream Editor' and it is a powerful utility that allows you to parse and convert text right from the command line.
What should you check before working with Use Sed Command in Linux?
Whether you are dealing with configuration files, scripts, or even plain text, sed is the tool that helps you manipulate text quickly and efficiently.
What can you do if the first method does not work?
The main use of sed is to search for specific patterns of text and replace them with something else.
Reader Comments 0
Sign in with email or Google to join the discussion.