Linux diff 命令,是否丢弃换行符?

Joh*_*ohn 2 diff

Linux 实现是否diff -w丢弃\n?我无法访问 Linux 机器来检查它。

ter*_*don 5

-w标志的手段

-w --ignore-all-space
    Ignore all white space.
Run Code Online (Sandbox Code Playgroud)

现在,这不是很清楚,因为“空白”实际上\n通常包括在内。例如,\sPerl Compatible Regular Expressions 或 POSIX 中的类[[:space:]]都匹配换行符:

$ printf '\n' | grep -zqP '\s' && echo yes
yes
$ printf '\n' | grep -zq '[[:space:]]' && echo yes
yes
Run Code Online (Sandbox Code Playgroud)

但是,diff通过比较和行是由\n字符定义的(grep实际上也是如此,这就是我必须在-z上面使用的原因)。因此,diff不能\n考虑因为\n意味着这是一条不同的线路。所以不,该-w选项不会导致diff丢弃换行符,空行表示文件不匹配:

$ printf 'foo\nbar\n' > file1
$ printf 'foo\n\nbar\n' > file2
$ diff -wq  file1 file2
Files file1 and file2 differ
Run Code Online (Sandbox Code Playgroud)

但是,diff由于行数,有一个选项确实可以忽略更改:

-B --ignore-blank-lines
    Ignore changes whose lines are all blank.

$ diff -sB  file1 file2
Files file1 and file2 are identical
Run Code Online (Sandbox Code Playgroud)