Linux下相同内容-文本和文件计算的摘要不一致

大部分java开发是在Windows上开发,而生产环境下在Linux中运行.
Windows/Dos 换行符 CRLF \r\n
Linux/Unix 换行符 LF \n
MacOS 换行符 CR \r
造成问题,在win下编辑的文本,上传Linux后文件内容每行内容会多出来^M$,在计算文本摘要和文件摘要就不一样了。

参考

Shell中去掉文件中的换行符简单方法
linux 如何删除文件中的空格和换行符号并保存到新文件中
linux删除文件中换行符的三种方法
linux统计文件每行长度,wc命令 – 统计文件的字节数、字数、行数
Windows和Linux的文件分隔符、换行符问题

问题

测试 文本test.txt,在win下编辑内容hello word,计算crc和md5不一致,只有截取后计算一致

[root@localhost ~]# wc -c test.txt 
12 test.txt
[root@localhost ~]# cat test.txt | md5sum
c95c2e9369b537420c0d35ef7ba4f110  -
[root@localhost ~]# echo 'hello word'|md5sum
4d2220fcf2abf3a9baac712bb93bd29c  -
[root@localhost ~]# cat test.txt |cksum
1699679688 12
[root@localhost ~]# echo 'hello word'|cksum
1125081862 11
[root@localhost ~]# cat test.txt | cut -b 1-10| cksum
1125081862 11
[root@localhost ~]# cat test.txt | cut -b 1-10| md5sum
4d2220fcf2abf3a9baac712bb93bd29c  -
[root@localhost ~]# cat -A test.txt 
hello word^M$
[root@localhost ~]# cat test.txt 
hello word

在这里插入图片描述

在Linux上创建文件是没问题的

[root@localhost ~]# echo 'hello word'|md5sum
4d2220fcf2abf3a9baac712bb93bd29c  -
[root@localhost ~]# echo 'hello word'|cksum
1125081862 11
[root@localhost ~]# echo 'hello word' > 2test.txt
[root@localhost ~]# cksum 2test.txt 
1125081862 11 2test.txt
[root@localhost ~]# md5sum 2test.txt 
4d2220fcf2abf3a9baac712bb93bd29c  2test.txt
[root@localhost ~]# wc -c 2test.txt 
11 2test.txt
[root@localhost ~]# cat 2test.txt 
hello word
[root@localhost ~]# cat -A 2test.txt 
hello word$

在这里插入图片描述

END

所以文件要么直接在Linux创建,要么计算的时候处理一下文件内容

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值