2022-12-13 04:53:24 +01:00
|
|
|
# sed
|
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
> GNU stream editor for filtering and transforming text.
|
2022-12-13 04:53:24 +01:00
|
|
|
> See also: `awk`, `ed`.
|
|
|
|
> More information: <https://www.gnu.org/software/sed/manual/sed.html>.
|
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
- Replace `apple` with `mango` on all lines using basic regex, print to `stdout`:
|
2022-12-13 04:53:24 +01:00
|
|
|
|
|
|
|
`{{command}} | sed 's/apple/mango/g'`
|
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
- Replace `apple` with `APPLE` on all lines using extended regex, print to `stdout`:
|
2024-01-24 23:07:17 -03:00
|
|
|
|
2025-03-12 21:05:58 +02:00
|
|
|
`{{command}} | sed {{[-E|--regexp-extended]}} 's/(apple)/\U\1/g'`
|
2024-01-24 23:07:17 -03:00
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
- Use basic regex to replace `apple` with `mango` and `orange` with `lime` in-place in a file (overwriting original file):
|
2024-01-24 23:07:17 -03:00
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
`sed {{[-i|--in-place]}} -e 's/apple/mango/g' -e 's/orange/lime/g' {{path/to/file}}`
|
2024-01-24 23:07:17 -03:00
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
- Execute a specific `sed` script file and print the result to `stdout`:
|
2022-12-13 04:53:24 +01:00
|
|
|
|
2025-03-26 02:30:50 +02:00
|
|
|
`{{command}} | sed {{[-f|--file]}} {{path/to/script.sed}}`
|
2022-12-13 04:53:24 +01:00
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
- [p]rint only the first line to `stdout`:
|
2022-12-13 04:53:24 +01:00
|
|
|
|
2025-03-12 21:05:58 +02:00
|
|
|
`{{command}} | sed {{[-n|--quiet]}} '1p'`
|
2022-12-13 04:53:24 +01:00
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
- [d]elete lines 1 to 5 of a file and back up the original file with a `.orig` extension:
|
2022-12-13 04:53:24 +01:00
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
`sed {{[-i|--in-place=]}}{{.orig}} '1,5d' {{path/to/file}}`
|
2022-12-13 04:53:24 +01:00
|
|
|
|
2025-06-18 16:39:12 +10:00
|
|
|
- [i]nsert a new line at the beginning of a file, overwriting the original file in-place:
|
2022-12-13 04:53:24 +01:00
|
|
|
|
2025-03-12 21:05:58 +02:00
|
|
|
`sed {{[-i|--in-place]}} '1i\your new line text\' {{path/to/file}}`
|
2025-06-18 16:39:12 +10:00
|
|
|
|
|
|
|
- Delete blank lines (with or without spaces/tabs) from a file, overwriting the original file in-place:
|
|
|
|
|
|
|
|
`sed {{[-i|--in-place]}} '/^[[:space:]]*$/d' {{path/to/file}}`
|