sed Command Examples in Linux¶
Introduction¶
These examples show practical ways to use sed on a Linux terminal. Each example is written so you can adapt it for administration or troubleshooting.
Example 1: Basic Usage¶
sed 's/old/new/' file.txt
This is the simplest form of the command and is a good starting point before adding options.
Example 2: Common Admin Task¶
sed -n '1,20p' /etc/ssh/sshd_config
This example reflects a common task on RHEL, Rocky Linux, AlmaLinux, or similar systems.
Example 3: Useful Option¶
sed '/debug/d' app.conf
This option helps narrow the result, change behavior, or handle a more realistic target.
Example 4: Real-World Scenario¶
sed -i.bak 's/enabled=false/enabled=true/' app.conf
Use this pattern when the task moves beyond a single basic command.
Example 5: Verification¶
sed --version
Example output:
https://example.com
https://cloudarks.com
Common Mistakes¶
- Using
sed -iwithout a backup on important files. - Forgetting the
gflag when every match on a line must be replaced. - Choosing a delimiter that conflicts with paths or URLs and makes the expression hard to read.
Quick Reference¶
sed 's/old/new/' file.txt
sed -n '1,20p' /etc/ssh/sshd_config
sed '/debug/d' app.conf
sed -i.bak 's/enabled=false/enabled=true/' app.conf
sed --version
Related Guides¶
Summary¶
Good sed usage means choosing the right option, keeping the target clear, and verifying the result with output you can explain.