The Power of the Stream Editor
If you need to change the word “localhost” to an actual IP address inside a single configuration file, opening nano or vim is perfectly fine. But what if you need to change that IP address across 500 different configuration files simultaneously? What if you need to delete the first three lines of a massive 10GB log file without loading the entire file into memory?
For these tasks, Linux administrators rely on sed (Stream Editor). sed is a non-interactive text editor. Instead of opening a visual interface, you pass sed a set of instructions via the command line. It reads the text stream (either from a file or piped from another command) line-by-line, applies your instructions, and outputs the result instantly.
The Basic Syntax: Substitution
The most common use of sed is finding and replacing text using the substitution command (s).
The basic syntax looks like this:
sed 's/find/replace/' filename.txt
s: Stands for substitute./: The delimiter separating the strings. (You can actually use any character, like|or:, which is helpful if your search string contains slashes, like a file path).
Example: Replacing a Word
To replace the word “apple” with “orange” in a file called fruits.txt:
sed 's/apple/orange/' fruits.txt
Important Note: By default, sed only replaces the first instance of the word on each line. If a line says “apple apple”, it will become “orange apple”.
The Global Flag (g)
To force sed to replace every instance of the word across the entire line, append the global (g) flag to the end of the command:
sed 's/apple/orange/g' fruits.txt
The In-Place Flag (-i)
By default, sed does not actually change your original file. It simply prints the modified text to your terminal screen. This is a brilliant safety feature.
Once you verify the output looks correct, add the -i (in-place) flag to write the changes permanently into the file:
sed -i 's/apple/orange/g' fruits.txt
Advanced Filtering and Deletion
sed isn’t just for replacing text; it can also selectively delete lines from a file without you ever opening it.
Deleting Specific Lines
If you know exactly which line is causing an error in a configuration file (e.g., line 15), you can delete it instantly using the d (delete) command:
sed -i '15d' config.cfg
To delete a range of lines (e.g., lines 1 through 10):
sed -i '1,10d' logfile.txt
Deleting Lines Based on a Pattern
Suppose you have a massive log file, and you want to strip out every line that contains the word “DEBUG” to make the file smaller and easier to read.
sed -i '/DEBUG/d' application.log
Using Regular Expressions
The true power of sed is unlocked when you combine it with Regular Expressions (Regex). This allows you to match complex patterns rather than exact words.
For example, if you want to uncomment a configuration file (removing the # at the very beginning of the line), you can use the caret symbol (^), which represents the start of a line:
sed -i 's/^#//' config.cfg
This tells sed: “Find a hash symbol, but only if it is the absolute first character on the line, and replace it with nothing.”
Conclusion
While the syntax can initially look like cryptic gibberish, mastering sed is a rite of passage for Linux power users. It allows you to perform massive, surgical text manipulations across thousands of files in milliseconds, completely automating the most tedious aspects of server administration.