gpt4 book ai didi

diff - AIX diff -b 坏了?

转载 作者:行者123 更新时间:2023-12-04 06:04:17 30 4
gpt4 key购买 nike

我正在尝试比较忽略回车符的文件 - diff -b 在任何其他 unix 上的表现令人钦佩。但是在这个 AIX 5.3 机器上:

    tst1:tst2$ od -c testfile
0000000 t h i s \r \n f i l e \r \n h a s \r
0000020 \n c a r r i a g e \r \n r e t u r
0000040 n s \r \n
0000044
tst1:tst2$ od -c testfile_nocr
0000000 t h i s \n f i l e \n h a s \n c a
0000020 r r i a g e \n r e t u r n s \n
0000037
tst1:tst2$ diff -b testfile testfile_nocr
1,5c1,5
< this
< file
< has
< carriage
< returns
---
> this
> file
> has
> carriage
> returns
tst1:tst2$

为什么这样?

最佳答案

AIX 5.3 diff 似乎没有考虑 \r作为空白。请参阅以下成绩单:

/home/pax: od -c file1
0000000 h e l l o \n t h e r e \n
0000014

/home/pax: od -c file2
0000000 h e l l o \r \n t h e r e \r \n
0000016

/home/pax: od -c file3
0000000 h e l l o \n t h e r e \n
0000016

/home/pax: od -c file4
0000000 h e l l o \t \n t h e r e \t \n
0000016

/home/pax: diff -b file1 file2
1,2c1,2
< hello
< there
---
> hello
> there

/home/pax: diff -b file1 file3
<<No output>>

/home/pax: diff -b file1 file4
<<No output>>
diff 的联机帮助页在这件事上有点迟钝,说 -b将“导致行尾的任意数量的空白被视为单个换行符......”但实际上并没有定义它认为是空白的内容。

根据上述成绩单,我认为空格和制表符可以,但回车不行。

解决此问题的一种方法是使用如下脚本自行删除回车符:
#!/usr/bin/ksh
# crdiff: diffs two files after changing \r\n to \n in both.
if [[ $# -ne 2 ]] ; then
echo 'Usage: crdiff <file> <file2>'
return 1
fi
perl -pne 's/\r$//' $1 >/tmp/xyzzy_$$_$1
perl -pne 's/\r$//' $2 >/tmp/xyzzy_$$_$2
diff -b /tmp/xyzzy_$$_$1 /tmp/xyzzy_$$_$2
rc=$?
rm -f /tmp/xyzzy_$$_$1 /tmp/xyzzy_$$_$2
return $rc

或者,您可以编译 GNU diff实用程序,并将其插入 /usr/bin 之前的路径中- 似乎没有问题 \r作为空白。事实上,自 diff 起,我们在 AIX4 机器上就是这样做的。甚至不假装支持 -b选项。

关于diff - AIX diff -b 坏了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8556395/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com