2014-05-30 21:01:43 +02:00
|
|
|
---
|
2017-09-21 10:30:07 +02:00
|
|
|
title: sed
|
2015-11-24 06:02:17 +01:00
|
|
|
category: CLI
|
2017-09-21 10:30:07 +02:00
|
|
|
intro: |
|
2020-06-23 16:15:39 +02:00
|
|
|
Here's some hints on using sed.
|
2014-05-30 21:01:43 +02:00
|
|
|
---
|
|
|
|
|
2024-02-14 21:43:19 +01:00
|
|
|
### Replacements
|
2014-05-30 21:01:43 +02:00
|
|
|
|
2024-02-14 21:43:19 +01:00
|
|
|
#### In-place replacement (GNU)
|
2014-05-30 21:01:43 +02:00
|
|
|
|
2017-09-21 10:30:07 +02:00
|
|
|
```bash
|
|
|
|
sed -i -e 's/foo/bar/' example.md
|
|
|
|
```
|
2014-05-30 21:01:43 +02:00
|
|
|
|
2020-07-05 13:02:32 +02:00
|
|
|
In GNU sed: use `-i` without arg.
|
|
|
|
|
|
|
|
#### In-place replacement (BSD)
|
2014-05-30 21:01:43 +02:00
|
|
|
|
2017-09-21 10:30:07 +02:00
|
|
|
```bash
|
|
|
|
sed -i '' -e 's/foo/bar/' example.md
|
|
|
|
```
|
2014-05-30 21:01:43 +02:00
|
|
|
|
2024-02-14 21:43:19 +01:00
|
|
|
In OSX, `-i ''` is required.
|
|
|
|
|
|
|
|
#### In-place Multiple replacements
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -i 's/match1/replace1/g; s/match2/replace2/g' \
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2024-03-15 14:23:32 +01:00
|
|
|
replace different matches with different values
|
2024-02-14 21:43:19 +01:00
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -i 's/\(MATCH1\|MATCH2\)/VALUE/g' \
|
|
|
|
|
|
|
|
```
|
|
|
|
|
2024-03-15 14:23:32 +01:00
|
|
|
replace multiple matches with the same value
|
2020-07-05 13:02:32 +02:00
|
|
|
|
|
|
|
## File regions
|
|
|
|
{:.-three-column}
|
2015-01-16 05:02:16 +01:00
|
|
|
|
2020-07-05 13:02:32 +02:00
|
|
|
### Print until a certain line is met
|
2015-01-16 05:02:16 +01:00
|
|
|
|
2017-09-21 10:30:07 +02:00
|
|
|
```bash
|
|
|
|
sed '/begin api/q'
|
|
|
|
```
|
2015-01-16 05:02:16 +01:00
|
|
|
|
2020-07-05 13:02:32 +02:00
|
|
|
### Print until a certain line is met, but not that line
|
2015-01-16 05:02:16 +01:00
|
|
|
|
2017-09-21 10:30:07 +02:00
|
|
|
```bash
|
|
|
|
sed '/^# begin/,$d'
|
|
|
|
```
|
2015-01-20 16:10:15 +01:00
|
|
|
|
2020-07-05 13:02:32 +02:00
|
|
|
### Print everything after a given line
|
2015-01-20 16:10:15 +01:00
|
|
|
|
2017-09-21 10:30:07 +02:00
|
|
|
```bash
|
|
|
|
sed -n '/end api/,$p'
|
|
|
|
```
|
2020-07-05 13:02:32 +02:00
|
|
|
|
|
|
|
Print after a given line is found.
|
|
|
|
|
|
|
|
### Print everything except matching
|
|
|
|
|
|
|
|
```bash
|
2022-11-01 04:19:16 +01:00
|
|
|
sed -n '/regex/!p'
|
2020-07-05 13:02:32 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Print everything except lines matching regex. Useful for printing files with comments.
|
2024-02-14 21:43:19 +01:00
|
|
|
|
|
|
|
#### Printing REGEX ranges
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -n -e '/^START$/,/^END$/p'
|
|
|
|
```
|
|
|
|
|
|
|
|
suppress output and print REGEX range
|
|
|
|
include (^START$,^END$) lines.
|
|
|
|
|
|
|
|
**OR** without "-n" (same result)
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -e '/^START$/,/^END$/p;d'
|
|
|
|
```
|
|
|
|
|
|
|
|
print REGEX range and delete other
|
|
|
|
output, the [;] character means run another
|
|
|
|
expression on the input file which is 'd' stands for delete .
|
|
|
|
|
|
|
|
### Append a text after a specific line number
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -e "1a ## HEADING 02:" README.md
|
|
|
|
```
|
|
|
|
|
|
|
|
this appends "## HEADING 02:" after the first line in the file README.md
|
|
|
|
and print the result to stdout replace -e with -i to write the file .
|
|
|
|
|
|
|
|
### Insert text before a specific line number
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -e "1i # HEADING 01:" README.md
|
|
|
|
```
|
|
|
|
|
|
|
|
the same as appending but before the first line.
|
|
|
|
|
|
|
|
### Deleting text
|
|
|
|
|
|
|
|
#### With line number
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -e "1,5d" README.md
|
|
|
|
```
|
|
|
|
|
|
|
|
delete a RANGE (i.e. including lines 1 to 5)
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -e '1,5!d' README.md
|
|
|
|
```
|
|
|
|
|
|
|
|
delete everything (i.e. excluding lines 1 to 5)
|
|
|
|
it is better to quote sed expressions with single quotes
|
|
|
|
especially when there is a [!] character.
|
|
|
|
|
|
|
|
#### With REGEX matching
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -e "/REGEX/Id" README.md
|
|
|
|
```
|
|
|
|
|
|
|
|
delete lines with /REGEX/ matched
|
|
|
|
/I is for insensitive search
|
|
|
|
|
|
|
|
```bash
|
|
|
|
sed -e '/REGEX/Ip;d' README.md
|
|
|
|
```
|
|
|
|
|
|
|
|
this invert the previous sed command
|
|
|
|
delete everything (excluding lines with REGEX)
|