271 lines
3.8 KiB
Markdown
271 lines
3.8 KiB
Markdown
# sed - Stream Editor
|
|
|
|
Der `sed`-Befehl ist ein Stream-Editor für Textmanipulation. Er eignet sich besonders für Suchen-und-Ersetzen-Operationen.
|
|
|
|
## Grundsyntax
|
|
|
|
```bash
|
|
sed [OPTIONEN] 'BEFEHL' [DATEI...]
|
|
```
|
|
|
|
## Suchen und Ersetzen
|
|
|
|
### Grundlegendes Ersetzen
|
|
|
|
```bash
|
|
sed 's/alt/neu/' datei.txt
|
|
```
|
|
|
|
Ersetzt erste Vorkommen in jeder Zeile.
|
|
|
|
### Alle Vorkommen in Zeile
|
|
|
|
```bash
|
|
sed 's/alt/neu/g' datei.txt
|
|
```
|
|
|
|
- `g` = global (alle Vorkommen)
|
|
|
|
### Datei direkt ändern
|
|
|
|
```bash
|
|
sed -i 's/alt/neu/g' datei.txt
|
|
```
|
|
|
|
- `-i` = in-place (Datei wird geändert)
|
|
|
|
### Backup vor Änderung
|
|
|
|
```bash
|
|
sed -i.bak 's/alt/neu/g' datei.txt
|
|
```
|
|
|
|
Erstellt `datei.txt.bak` vor Änderung.
|
|
|
|
## Zeilen auswählen
|
|
|
|
### Bestimmte Zeile
|
|
|
|
```bash
|
|
sed -n '5p' datei.txt # Nur Zeile 5
|
|
sed '5d' datei.txt # Zeile 5 löschen
|
|
sed '5s/alt/neu/' datei.txt # Nur in Zeile 5 ersetzen
|
|
```
|
|
|
|
### Zeilenbereich
|
|
|
|
```bash
|
|
sed -n '10,20p' datei.txt # Zeilen 10-20 anzeigen
|
|
sed '10,20d' datei.txt # Zeilen 10-20 löschen
|
|
```
|
|
|
|
### Letzte Zeile
|
|
|
|
```bash
|
|
sed -n '$p' datei.txt # Letzte Zeile
|
|
```
|
|
|
|
### Muster-basiert
|
|
|
|
```bash
|
|
sed '/^#/d' datei.txt # Zeilen mit # am Anfang löschen
|
|
sed '/ERROR/s/foo/bar/' datei.txt # Nur in Zeilen mit ERROR
|
|
```
|
|
|
|
## Zeilen einfügen und anhängen
|
|
|
|
### Vor Zeile einfügen
|
|
|
|
```bash
|
|
sed '3i\Neue Zeile' datei.txt
|
|
```
|
|
|
|
### Nach Zeile anhängen
|
|
|
|
```bash
|
|
sed '3a\Neue Zeile' datei.txt
|
|
```
|
|
|
|
### Nach Muster
|
|
|
|
```bash
|
|
sed '/TODO/a\WICHTIG: Bitte bearbeiten' datei.txt
|
|
```
|
|
|
|
## Zeilen löschen
|
|
|
|
### Leere Zeilen
|
|
|
|
```bash
|
|
sed '/^$/d' datei.txt
|
|
```
|
|
|
|
### Kommentare
|
|
|
|
```bash
|
|
sed '/^#/d' datei.txt
|
|
```
|
|
|
|
### Mehrere Muster
|
|
|
|
```bash
|
|
sed -e '/^#/d' -e '/^$/d' datei.txt
|
|
```
|
|
|
|
## Reguläre Ausdrücke
|
|
|
|
### Erweiterte Regex
|
|
|
|
```bash
|
|
sed -E 's/[0-9]{3}-[0-9]{4}/XXX-XXXX/g' datei.txt
|
|
```
|
|
|
|
- `-E` = Extended Regex
|
|
|
|
### Gruppen und Backreferences
|
|
|
|
```bash
|
|
sed 's/\([0-9]*\)-\([0-9]*\)/\2-\1/' datei.txt
|
|
```
|
|
|
|
Vertauscht Zahlen vor und nach Bindestrich.
|
|
|
|
Mit `-E`:
|
|
|
|
```bash
|
|
sed -E 's/([0-9]*)-([0-9]*)/\2-\1/' datei.txt
|
|
```
|
|
|
|
## Praktische Beispiele
|
|
|
|
### Konfigurationsdatei ändern
|
|
|
|
```bash
|
|
sed -i 's/^#Port 22/Port 2222/' /etc/ssh/sshd_config
|
|
```
|
|
|
|
### IP-Adressen ersetzen
|
|
|
|
```bash
|
|
sed 's/192\.168\.1\./10.0.0./g' config.txt
|
|
```
|
|
|
|
### Zeilennummern hinzufügen
|
|
|
|
```bash
|
|
sed = datei.txt | sed 'N;s/\n/\t/'
|
|
```
|
|
|
|
### Kommentare und Leerzeilen entfernen
|
|
|
|
```bash
|
|
sed -e '/^#/d' -e '/^$/d' config.conf
|
|
```
|
|
|
|
### Mehrere Ersetzungen
|
|
|
|
```bash
|
|
sed -e 's/foo/bar/g' -e 's/alt/neu/g' datei.txt
|
|
```
|
|
|
|
### Case-insensitive Suche
|
|
|
|
```bash
|
|
sed 's/fehler/ERROR/gI' datei.txt
|
|
```
|
|
|
|
- `I` = case-insensitive
|
|
|
|
### Zeilen mit Muster extrahieren
|
|
|
|
```bash
|
|
sed -n '/ERROR/p' logfile.txt
|
|
```
|
|
|
|
Zeigt nur Zeilen mit "ERROR".
|
|
|
|
### Text zwischen Mustern
|
|
|
|
```bash
|
|
sed -n '/START/,/END/p' datei.txt
|
|
```
|
|
|
|
### Zeilen nummerieren
|
|
|
|
```bash
|
|
sed = datei.txt
|
|
```
|
|
|
|
## Mit Pipes
|
|
|
|
```bash
|
|
cat datei.txt | sed 's/alt/neu/g' | grep wichtig
|
|
ps aux | sed -n '1p;/firefox/p'
|
|
```
|
|
|
|
## Mehrere Dateien
|
|
|
|
```bash
|
|
sed -i 's/alt/neu/g' *.txt
|
|
```
|
|
|
|
## Sed-Skripte
|
|
|
|
**script.sed:**
|
|
```sed
|
|
# Kommentare entfernen
|
|
/^#/d
|
|
# Leerzeilen entfernen
|
|
/^$/d
|
|
# foo durch bar ersetzen
|
|
s/foo/bar/g
|
|
```
|
|
|
|
Verwenden:
|
|
|
|
```bash
|
|
sed -f script.sed datei.txt
|
|
```
|
|
|
|
## Fehler vermeiden
|
|
|
|
### Backup erstellen
|
|
|
|
```bash
|
|
sed -i.bak 's/alt/neu/g' wichtig.txt
|
|
```
|
|
|
|
### Testen ohne Änderung
|
|
|
|
```bash
|
|
sed 's/alt/neu/g' datei.txt > test.txt
|
|
diff datei.txt test.txt
|
|
```
|
|
|
|
### Spezialzeichen escapen
|
|
|
|
```bash
|
|
# Punkt ist Wildcard
|
|
sed 's/192.168.1.1/10.0.0.1/' config # FALSCH
|
|
|
|
# Punkt escapen
|
|
sed 's/192\.168\.1\.1/10.0.0.1/' config # RICHTIG
|
|
```
|
|
|
|
## Alternative Delimiter
|
|
|
|
Wenn Pfade mit `/` vorkommen:
|
|
|
|
```bash
|
|
sed 's|/old/path|/new/path|g' datei.txt
|
|
```
|
|
|
|
Statt `/` kann `|`, `#` oder andere Zeichen verwendet werden.
|
|
|
|
## Siehe auch
|
|
|
|
- [[grep]] - Textsuche
|
|
- [[awk]] - Komplexere Textverarbeitung
|
|
- [[find]] - Dateien finden und mit sed bearbeiten
|
|
- [[BASH Funktionen]] - sed in Funktionen verwenden
|