Overview
sed
stands for Stream Editor. It is a command that is used for text-processing tool that allows us to perform various operations on text files and stream of text data. sed
is useful for tasks like search and replace, text transformation, and filtering.sed
works by reading text input line by line and applying specified commands to each line of text. These commands are typically expressed as “sed scripts”, which consist of one or more SED commands that tell sed
what to do with the text.Here are some common tasks that you can perform with SED:
- Search and Replace: we can use sed to search for a spesific pattern in a text file and replace it with another pattern. For example:
sed 's/old-text/new-text/g' input.txt > output.txt
- Text Transformations: SED can perform various text transformations like converting text to uppercase or lowercase, removing or adding characters, and more.
- Selective Printing: SED allows you to print only specific lines from a text file based on patterns or line numbers.
- Deleting Lines: You can use SED to delete specific lines from a text file based on patterns or line numbers.
- Text Filtering: SED can filter text based on certain conditions and print only lines that meet those conditions.
Usage Example
- Search and Replace:
Let's say you have a text file called
data.txt
with the following content, and you want to replace all occurrences of "apple" with "banana."
This is an apple. There are many apples in the apple orchard.
You can use
sed
to perform this replacement:sed 's/apple/banana/g' data.txt
Output:
This is an banana. There are many banana in the banana orchard.
- Text Transformation:
Suppose you have a file with text in uppercase, and you want to convert it to lowercase. You can use
sed
for this:
cat input.txt | sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/'
This command reads from
input.txt
, converts all uppercase letters to lowercase, and prints the result to the console.- Selective Printing: If you have a large log file and want to extract lines that contain a specific keyword, use this:
sed -n '/error/p' logfile.txt
This command prints all lines containing the word "error" from
logfile.txt
.- Text Filtering: Let's say you have a CSV file, and you want to filter rows where the third column contains a specific value. For example, you want to extract all rows where the third column is "42":
sed -n '/^[^,]*,[^,]*,42,/p' data.csv
This command will print all rows where the third column is "42" from
data.csv
.- Deleting Lines:
If you want to remove lines that contain a certain pattern, you can use
sed
. For example, to remove all lines containing the word "obsolete" from a file:
sed '/obsolete/d' input.txt
This command will print the contents of
input.txt
with all lines containing "obsolete" removed.These are just a few examples of what you can do with
sed
. It's a versatile tool for text manipulation and can be combined with other Linux commands and scripts to perform more complex text-processing tasks efficiently.