Advanced sed: multi-line editing, in-place replacement, complex patterns.
medium risk
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.
| Flag | Meaning |
|---|---|
-i | Edit file in-place (-i.bak to backup) |
-n | Suppress automatic print (use with p) |
-e | Add script expression |
-r/-E | Extended regular expressions |
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 →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.
Reference text is hand-written and may be incomplete — check man sed for your version. Test destructive commands on a non-production system first.