# Referensi Cepat sed

*Pengeditan stream, substitusi, address, hold space, transformasi in-place*

> Source: GNU sed Manual (gnu.org/software/sed) · MIT

## Dasar

### Menjalankan sed

```
sed 's/old/new/' file.txt          # substitute first match per line
sed 's/old/new/g' file.txt         # substitute all matches per line
sed -n '5p' file.txt               # print only line 5
sed '3d' file.txt                  # delete line 3
echo "hello" | sed 's/hello/hi/'   # pipe input
```

### Flag Command-Line

| Command | Description |
|---------|-------------|
| `-n` | Sembunyikan output otomatis; hanya cetak dengan `p` |
| `-e 'cmd'` | Jalankan perintah sed (gabungkan beberapa dengan `-e`) |
| `-f script.sed` | Baca perintah dari file |
| `-i[suffix]` | Edit file langsung (opsional suffix backup) |
| `-E / -r` | Gunakan extended regular expression |

## Substitusi

### Sintaks Substitusi

```
sed 's/foo/bar/' f                 # first occurrence per line
sed 's/foo/bar/g' f                # all occurrences
sed 's/foo/bar/3' f                # 3rd occurrence only
sed 's/foo/bar/gi' f               # all, case-insensitive
sed 's|/usr/bin|/opt/bin|g' f      # alternate delimiter
```

### Flag Substitusi

| Command | Description |
|---------|-------------|
| `g` | Ganti semua kemunculan pada baris |
| `N (number)` | Ganti hanya kemunculan ke-N |
| `p` | Cetak baris jika substitusi berhasil |
| `w file` | Tulis baris hasil substitusi ke file |
| `i / I` | Pencocokan tanpa memperhatikan huruf besar/kecil (GNU) |

## Address

### Contoh Address

```
sed '3s/a/b/' f                    # only on line 3
sed '2,5s/a/b/' f                  # lines 2 through 5
sed '/^#/d' f                      # delete lines starting with #
sed '/start/,/end/d' f             # delete range between patterns
sed '1~2d' f                       # delete odd-numbered lines (GNU)
```

### Jenis Address

| Command | Description |
|---------|-------------|
| `N` | Nomor baris N |
| `$` | Baris terakhir |
| `N,M` | Rentang baris dari N ke M |
| `/regex/` | Baris yang cocok dengan regex |
| `/regex1/,/regex2/` | Rentang dari kecocokan pertama ke kedua |
| `N~step` | Mulai dari N, setiap baris ke-step (GNU) |
| `addr!` | Negasi — terapkan ke baris yang tidak cocok |

## Hapus & Cetak

### Perintah Hapus & Cetak

```
sed '5d' f                         # delete line 5
sed '/^$/d' f                      # delete blank lines
sed -n '10,20p' f                  # print lines 10–20
sed -n '/error/p' f                # print lines matching pattern
sed '/debug/!d' f                  # keep only matching lines
```

### Referensi Perintah

| Command | Description |
|---------|-------------|
| `d` | Hapus pattern space, mulai siklus berikutnya |
| `D` | Hapus hingga newline pertama di pattern space |
| `p` | Cetak pattern space |
| `P` | Cetak hingga newline pertama di pattern space |
| `q` | Keluar setelah mencetak pattern space saat ini |
| `Q` | Keluar tanpa mencetak (GNU) |

## Insert & Append

### Insert, Append, Change

```
sed '3i\inserted line' f           # insert before line 3
sed '3a\appended line' f           # append after line 3
sed '3c\replaced line' f           # replace line 3
sed '/marker/a\new line' f         # append after pattern match
```

### Perintah

| Command | Description |
|---------|-------------|
| `i\text` | Sisipkan teks sebelum baris saat ini |
| `a\text` | Tambahkan teks setelah baris saat ini |
| `c\text` | Ganti baris saat ini dengan teks |
| `r file` | Baca dan tambahkan isi file |
| `R file` | Baca dan tambahkan satu baris dari file (GNU) |
| `w file` | Tulis pattern space ke file |

## Hold Space

### Perintah Hold Space

| Command | Description |
|---------|-------------|
| `h` | Salin pattern space ke hold space |
| `H` | Tambahkan pattern space ke hold space |
| `g` | Salin hold space ke pattern space |
| `G` | Tambahkan hold space ke pattern space |
| `x` | Tukar pattern space dan hold space |

### Contoh Hold Space

```
sed -n '1!G;h;$p' f               # reverse lines (tac)
sed '/^$/{ x; s/\n//; x; }' f     # collapse hold on blank
sed -n 'H;${x;s/\n/ /g;p;}' f     # join all lines with space
```

## Beberapa Perintah

### Menggabungkan Perintah

```
sed -e 's/foo/bar/g' -e 's/baz/qux/g' f
sed 's/foo/bar/g; s/baz/qux/g' f
sed '/header/{ s/old/new/; s/foo/bar/; }' f
sed -f commands.sed input.txt
```

### Pengelompokan & Percabangan

| Command | Description |
|---------|-------------|
| `{ cmd1; cmd2; }` | Kelompokkan perintah untuk address yang sama |
| `:label` | Definisikan label cabang |
| `b label` | Lompat ke label |
| `t label` | Lompat jika `s///` terakhir berhasil |
| `T label` | Lompat jika `s///` terakhir gagal (GNU) |

## Pengeditan In-place

### Contoh In-place

```
sed -i 's/old/new/g' file.txt            # edit in place (GNU)
sed -i.bak 's/old/new/g' file.txt        # backup as file.txt.bak
sed -i '' 's/old/new/g' file.txt         # macOS in-place (no backup)
sed -i '/^#/d' config.txt                # remove comments in place
```

### Catatan Platform

| Command | Description |
|---------|-------------|
| `GNU sed -i` | Suffix opsional; `-i` saja mengedit tanpa backup |
| `BSD/macOS sed -i` | Membutuhkan argumen suffix; gunakan `-i ''` tanpa backup |
| `-i.bak` | Membuat file backup dengan ekstensi `.bak` |
| `Multiple files` | `sed -i 's/a/b/g' *.txt` mengedit semua file yang cocok |

## Regex

### Regex dalam sed

| Command | Description |
|---------|-------------|
| `.` | Karakter apa pun (satu) |
| `*` | Nol atau lebih dari ekspresi sebelumnya |
| `\+` | Satu atau lebih (BRE) — `+` di ERE |
| `\?` | Nol atau satu (BRE) — `?` di ERE |
| `^` | Awal baris |
| `$` | Akhir baris |
| `[abc]` | Kelas karakter |
| `\( \)` | Capture group (BRE) — `()` di ERE |
| `\1, \2` | Referensi balik ke capture group |
| `&` | Seluruh string yang cocok (dalam penggantian) |

### Contoh Regex

```
sed 's/[0-9]\+/NUM/g' f               # replace numbers
sed -E 's/(foo)(bar)/\2\1/g' f         # swap groups (ERE)
sed 's/.*/(&)/' f                      # wrap line in parens
sed 's/[ \t]*$//' f                    # strip trailing whitespace
```

## Pola Umum

### One-Liner

```
sed -n '1p' f                          # first line (head -1)
sed '$!d' f                            # last line (tail -1)
sed '/^$/d' f                          # remove blank lines
sed 's/^[ \t]*//' f                    # strip leading whitespace
sed '=' f | sed 'N;s/\n/\t/' f         # number lines
```

### Resep

| Command | Description |
|---------|-------------|
| `Double-space` | `sed G` — tambahkan baris kosong setelah setiap baris |
| `Hapus tag HTML` | `sed 's/<[^>]*>//g'` |
| `Ekstrak email` | `sed -nE 's/.*([a-z]+@[a-z.]+).*/\1/p'` |
| `Tambah komentar` | `sed 's/^/# /'` — awali setiap baris dengan `# ` |
| `Hapus baris kosong di akhir` | `sed -e :a -e '/^\n*$/{$d;N;ba' -e '}'` |
| `Ganti baris ke-N` | `sed 'Nc\new text'` — ganti baris N |
