2025-03-14 21:59:59 -06:00
|
|
|
# sed
|
|
|
|
|
|
2025-07-24 22:27:13 -06:00
|
|
|
> GNU stream editor for filtering and transforming text.
|
2025-03-14 21:59:59 -06:00
|
|
|
> See also: `awk`, `ed`.
|
|
|
|
|
> More information: <https://www.gnu.org/software/sed/manual/sed.html>.
|
|
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
- [s]ubstitute all occurrences of "apple" with "mango" on all lines, print to `stdout`:
|
2025-03-14 21:59:59 -06:00
|
|
|
|
|
|
|
|
`{{command}} | sed 's/apple/mango/g'`
|
|
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
- Replace "apple" with "mango" in-place in a file (overwriting original file):
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
`sed {{[-i|--in-place]}} 's/apple/mango/g' {{path/to/file}}`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
- Run multiple substitutions in one command:
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
`{{command}} | sed -e '{{s/apple/mango/g}}' -e '{{s/orange/lime/g}}'`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
- Use a custom delimiter (useful when the pattern contains slashes):
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
`{{command}} | sed '{{s#////#____#g}}'`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
- [d]elete lines 1 to 5 of a file and back up the original file with a `.orig` extension:
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
`sed {{[-i|--in-place=]}}.orig '1,5d' {{path/to/file}}`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
- [p]rint only the first line to `stdout`:
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
`{{command}} | sed {{[-n|--quiet]}} '1p'`
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
- [i]nsert a new line at the beginning of a file, overwriting the original file:
|
2025-03-14 21:59:59 -06:00
|
|
|
|
2025-03-19 19:23:44 -06:00
|
|
|
`sed {{[-i|--in-place]}} '1i\your new line text\' {{path/to/file}}`
|
2025-07-24 22:27:13 -06:00
|
|
|
|
2026-02-18 06:55:01 -07:00
|
|
|
- Delete blank lines (with or without spaces/tabs) from a file, overwriting the original file:
|
2025-07-24 22:27:13 -06:00
|
|
|
|
|
|
|
|
`sed {{[-i|--in-place]}} '/^[[:space:]]*$/d' {{path/to/file}}`
|