CRLF entfernen

Apr. 23, 2012·
Julio Batista Silva
Julio Batista Silva
· 4 Min Lesezeit

Aus historischen Gründen (Teletype) verwendet DOS/Windows zwei Steuerzeichen für den Zeilenumbruch: Carriage Return (CR = 0x0D) und Line Feed (LF = 0x0A). Unix‑ähnliche Systeme erkannten den Overhead und nutzen nur LF für eine neue Zeile.

Beispiel:

$ cat bacon.txt
Bacon Ipsum:

Bacon ipsum dolor sit amet salami
pork belly tail tongue pancetta,
pork loin tri-tip drumstick bresaola shankle.
$ file bacon_windows.txt bacon_linux.txt
bacon_windows.txt: ASCII text, with CRLF line terminators
bacon_linux.txt:   ASCII text
$ hexdump -C bacon_linux.txt
00000000  42 61 63 6f 6e 20 49 70  73 75 6d 3a <strong>0a 0a</strong> 42 61  |Bacon Ipsum:..Ba|
00000010  63 6f 6e 20 69 70 73 75  6d 20 64 6f 6c 6f 72 20  |con ipsum dolor |
00000020  73 69 74 20 61 6d 65 74  20 73 61 6c 61 6d 69 <strong>0a</strong>  |sit amet salami.|
00000030  70 6f 72 6b 20 62 65 6c  6c 79 20 74 61 69 6c 20  |pork belly tail |
00000040  74 6f 6e 67 75 65 20 70  61 6e 63 65 74 74 61 2c  |tongue pancetta,|
00000050  <strong>0a</strong> 70 6f 72 6b 20 6c 6f  69 6e 20 74 72 69 2d 74  |.pork loin tri-t|
00000060  69 70 20 64 72 75 6d 73  74 69 63 6b 20 62 72 65  |ip drumstick bre|
00000070  73 61 6f 6c 61 20 73 68  61 6e 6b 6c 65 2e <strong>0a</strong>     |saola shankle..|
0000007f
$ hexdump -C bacon_windows.txt
00000000  42 61 63 6f 6e 20 49 70  73 75 6d 3a <strong>0d 0a 0d 0a</strong>  |Bacon Ipsum:....|
00000010  42 61 63 6f 6e 20 69 70  73 75 6d 20 64 6f 6c 6f  |Bacon ipsum dolo|
00000020  72 20 73 69 74 20 61 6d  65 74 20 73 61 6c 61 6d  |r sit amet salam|
00000030  69 <strong>0d 0a</strong> 70 6f 72 6b 20  62 65 6c 6c 79 20 74 61  |i..pork belly ta|
00000040  69 6c 20 74 6f 6e 67 75  65 20 70 61 6e 63 65 74  |il tongue pancet|
00000050  74 61 2c <strong>0d 0a</strong> 70 6f 72  6b 20 6c 6f 69 6e 20 74  |ta,..pork loin t|
00000060  72 69 2d 74 69 70 20 64  72 75 6d 73 74 69 63 6b  |ri-tip drumstick|
00000070  20 62 72 65 73 61 6f 6c  61 20 73 68 61 6e 6b 6c  | bresaola shankl|
00000080  65 2e <strong>0d 0a</strong>                                       |e...|
00000084

Windows‑Datei zeigt 0d 0a, Linux nur 0a – das führt zu Kompatibilitätsproblemen.

Gute Editoren kommen damit klar, aber Tools wie diff sehen die Dateien als unterschiedlich:

$ diff bacon_windows.txt bacon_linux.txt
1,5c1,5
< Bacon Ipsum:
<
< Bacon ipsum dolor sit amet salami
< pork belly tail tongue pancetta,
< pork loin tri-tip drumstick bresaola shankle.
---
> Bacon Ipsum:
>
> Bacon ipsum dolor sit amet salami
> pork belly tail tongue pancetta,
> pork loin tri-tip drumstick bresaola shankle.

Ich habe gemischte Dateien in die Versionskontrolle gelegt und auf LF standardisiert. So geht die Konvertierung unter Linux:

Vim

Zwei Wege:

1

  • CRLF als ^M anzeigen: :e ++ff=unix.
  • Alle ^M durch ^N ersetzen: :s/\r/\r/.
  • ^M nur am Zeilenende entfernen: :s/\r\+$//
  • Alle ^M entfernen: :s/^M//

^M tippt man als Ctrl+V dann Ctrl+M.

2

  • Dateiformat auf unix setzen: :setlocal fileformat=unix
  • Speichern: :w
  • Neu laden: :e

Mehrere Dateien konvertieren

  • DOS als Format annehmen: :set ffs=dos.
  • Liste der zu konvertierenden Dateien: :args *.c *.h.
  • Für jedes Argument das Format ändern: :argdo set ff=unix|w.

Weitere nützliche Optionen: :set list und :set nobomb.

sed

sed arbeitet zeilenweise. Wenn die Datei im DOS‑Format ist, kann man die letzten zwei Zeichen (CRLF) je Zeile durch LF ersetzen:

sed -i 's/.$//' arquivo.txt

Vorsicht: Ist die Datei bereits im Unix‑Format, würdest du damit das letzte Zeichen jeder Zeile löschen.

Die Gegenrichtung (Unix → DOS) mit GNU sed:

sed -i 's/$/\r/' arquivo.txt

Tofrodos

Wenn du den Status nicht prüfen willst, nutze Tofrodos – bereits konvertierte Dateien lässt es in Ruhe. Praktisch in Skripten.

  1. Installieren: Tofrodos (AUR) oder dos2unix.

  2. In der Projektwurzel ausführen (hier steht ^M für Ctrl+V + Ctrl+M):

    grep -IUrl --color '^M' . | xargs -ifile fromdos 'file'
    

Konvertiert alle CR+LF zu LF rekursiv.

Für die Gegenrichtung fromdos durch todos ersetzen.


Julio Batista Silva
Autoren
Senior Cloud-Entwickler
comments powered by Disqus