一套(DOS) ANSI-C 文本文件(点击此处下载)被复制到 OSX High Sierra 笔记本电脑。我想确保 CR\LF 不会成为编译文件的障碍。 Sublime 和 vi 似乎可以正确显示这些文件。
三个终结者是:
- 默认情况下,Mac 使用单个回车符 (CR),表示为 \r。
- 另一方面,Unix 使用单个换行符 (LF),\n。
- Windows 更进一步,使用两者,创建一个 (CRLF) 组合 \r\n。
dos2unix
已安装:
brew install dos2unix
尝试处理 ANSI-C 文件返回一个错误:
$ dos2unix *.h
dos2unix: Binary symbol 0x1A found at line 75
dos2unix: Skipping binary file error.h
dos2unix: Binary symbol 0x1A found at line 156
dos2unix: Skipping binary file random.h
dos2unix: Binary symbol 0x1A found at line 49
dos2unix: Skipping binary file resource.h
dos2unix: Binary symbol 0x1A found at line 107
dos2unix: Skipping binary file results.h
dos2unix: Binary symbol 0x1A found at line 261
dos2unix: Skipping binary file sim_lib.h
dos2unix: Binary symbol 0x1A found at line 27
dos2unix: Skipping binary file stats.h
.c 文件
$ dos2unix *.c
dos2unix: Binary symbol 0x1A found at line 110
dos2unix: Skipping binary file error.c
dos2unix: Binary symbol 0x1A found at line 780
dos2unix: Skipping binary file random.c
dos2unix: Binary symbol 0x1A found at line 79
dos2unix: Skipping binary file resource.c
dos2unix: Binary symbol 0x1A found at line 582
dos2unix: Skipping binary file results.c
dos2unix: Binary symbol 0x1A found at line 1755
dos2unix: Skipping binary file sim_lib.c
dos2unix: Binary symbol 0x1A found at line 102
dos2unix: Skipping binary file stats.c
问题
确保应用正确的终止符需要采取哪些步骤?
如何可视化(验证)哪个终结者正在参与? (CR\LF\CRLF)
更新
-f(强制)标志将忽略 0x1A 并处理该文件。递归查找.c文件:
user@hostname:~/csim$ find . -name '*.c' | xargs dos2unix -f
答案1
这是一个简单的测试:
~/tmp$ printf "\x1Aabc\r\n" > test
~/tmp$ od -a test
0000000 sub a b c cr nl
0000006
~/tmp$ dos2unix test
dos2unix: Binary symbol 0x1A found at line 1
dos2unix: Skipping binary file test
~/tmp$ dos2unix -f test
dos2unix: converting file test to Unix format...
~/tmp$ od -a test
0000000 sub a b c nl
0000005
。用于dos2unix -f
强制转换
。用于od -a
查看二进制文件
。 Mac 上的终结者也是 LF。