If you work with Linux, sooner or later you'll meet the mighty sed
. It's a stream editor β meaning it processes text line by line β and it's incredibly useful for everything from simple search and replace to complex text manipulation.
Letβs dive deep, but crisp, into sed
βs most powerful features. π
π What is sed
?
sed
stands for Stream EDitor. It reads input line-by-line, applies editing rules, and outputs the result. It doesnβt change the original file unless told to.
π₯ Basic Syntax:
sed [OPTIONS] 'command' file
π Common sed
Options
Option | Description |
---|---|
-n |
Suppress automatic printing (useful with p ) |
-e |
Add multiple commands |
-i |
Edit file in-place (use with caution) |
-r or -E
|
Enable extended regex (more powerful patterns) |
π οΈ Most Used sed
Commands
1. π Substitute (s)
sed 's/old/new/' file
- Replaces first occurrence of
old
withnew
on each line.
Flags:
-
g
= replace all occurrences in a line -
i
= case-insensitive -
p
= print matched lines (use with-n
)
sed 's/foo/bar/g' file # Replace all foo with bar
sed -n 's/foo/bar/p' file # Only print lines where replacement happened
2. π§Ή Delete Lines
sed '2d' file # Delete 2nd line
sed '1,3d' file # Delete lines 1 to 3
sed '/^$/d' file # Delete empty lines
sed '/pattern/d' file # Delete lines matching pattern
3. π¨οΈ Print Lines
sed -n '2p' file # Print only line 2
sed -n '3,5p' file # Print lines 3 to 5
sed -n '/error/p' file # Print lines matching "error"
4. π§΅ Insert, Append, Change
sed '2i\New line' file # Insert before line 2
sed '2a\New line' file # Append after line 2
sed '2c\Changed line' file # Replace line 2 entirely
5. π Multiple Commands
sed -e 's/foo/bar/' -e 's/baz/qux/' file
# OR using curly braces
sed '1,3{ s/foo/bar/; s/baz/qux/ }' file
π― Address Ranges
You can target lines using:
- Specific numbers:
1,5
- Patterns:
/start/,/end/
sed '/BEGIN/,/END/d' file # Delete everything from BEGIN to END
π§ Cool sed
Tricks
β 1. Replace only Nth occurrence on each line
sed 's/foo/bar/2' file # Replace 2nd occurrence only
β 2. Replace text in-place
sed -i 's/foo/bar/g' file # Make permanent change to file
β 3. Remove leading/trailing whitespace
sed 's/^[ \t]*//;s/[ \t]*$//' file
β 4. Comment out all lines matching a pattern
sed '/pattern/s/^/#/' file
β 5. Add line numbers
nl file | sed 's/^[ \t]*//' # or:
sed = file | sed 'N;s/\n/ /'
π Real-Life Examples
π‘ Replace a word in a config file
sed -i 's/localhost/127.0.0.1/g' config.cfg
π‘ Delete all blank lines
sed '/^$/d' notes.txt
π‘ Change HTML tags
sed 's/<b>/<strong>/g; s#</b>#</strong>#g' file.html
π§ͺ Testing sed
Quickly
Use <<<
to pass text directly:
sed 's/dog/cat/' <<< "I love my dog"
π§― Safety Tip
When using -i
, backup first!
sed -i.bak 's/error/ERROR/g' logfile.log
π Final Words
sed
is a scripting powerhouse. Start small β learn to replace, delete, and print β then go wild with multi-line editing, regex, and batch replacements.
Whether you're cleaning logs, transforming data, or tweaking config files, sed
is your Linux Swiss Army knife. πͺ
π¬ Got More sed
Magic?
Drop your favorite sed
one-liner in the comments!
Top comments (0)