sed advanced (advanced)

Advanced sed: multi-line editing, in-place replacement, complex patterns.

medium risk

interactive breakdown — edit the command and press Explain
$ 
⚠ Risks & cautions
What it does

Beyond simple substitution, sed supports in-place file editing, multi-line patterns, address ranges, and script files. The -i flag is essential for automated config management — patching configuration files without opening an editor.

Common flags
FlagMeaning
-iEdit file in-place (-i.bak to backup)
-nSuppress automatic print (use with p)
-eAdd script expression
-r/-EExtended regular expressions
Examples
sed -i.bak 's/old_value/new_value/g' config.iniReplace string in-place with backup · explain & risk-check →
sed -n '10,20p' large_file.logPrint only lines 10-20 · explain & risk-check →
sed '/^#/d; /^$/d' config.confRemove comments and blank lines · explain & risk-check →
sed -i 's/^SELINUX=enforcing/SELINUX=permissive/' /etc/selinux/configPatch a config file in-place · explain & risk-check →
sed -n '/ERROR/p' app.logPrint only lines containing ERROR · explain & risk-check →
Pro tip

Always use `-i.bak` in scripts for in-place edits — creates a backup automatically. Remove backup files with `find . -name '*.bak' -delete` after confirming changes.

Related
awk advanced grep patch
Open the full explainer →More in Text Processing

Reference text is hand-written and may be incomplete — check man sed for your version. Test destructive commands on a non-production system first.