sed
The stream editor, which applies editing commands such as find-and-replace to each line of text passing through it.
In more detail
s/old/new/ replaces the first match on each line and s/old/new/g every match. -n with p prints only the lines you select. The patterns are regular expressions, and the whole program should be in single quotes so the shell leaves it alone.
Examples
sed 's/error/ERROR/g' app.logsed -n '2,4p' poem.txtpython3 dora_metrics.py fixture | sed -n '4p'sed s/second/SECOND/ c.txtsed "s|^hosts/||" strips a prefix from every line.Related terms
- regular expression — A pattern language for matching text, where characters such as ., *, ^ and $ have special meanings.
- tr — The command that replaces, squeezes or deletes single characters as text passes through it.
- awk — A small programming language for text that splits each line into fields and runs actions on the lines that match a condition.
- tail — The command that prints the last lines of its input, ten by default or as many as -n says.
- grep — The command that prints the lines of its input that match a pattern.
Taught in
- Reshaping Text: cut, sort, uniq, tr, sed, awk — Bash Scripting & Command Line Automation
- Restoration: Failed Deployment Recovery Time — DevOps & Site Reliability Engineering
- What A Commit Really Is — Advanced Git Internals & Distributed Workflows
- Containment & Eradication: Host, Net & Identity — Security Operations & Threat Hunting (SOC)