处理文件CRLF line terminators的问题

对比file1.log和file2.log中相同重复的行。

发现很容易实现,直接:

$ for i in `cat file1.log`; do grep $i ./file2.log; done > sameline.log

结果就发现只有一条,研发的同学说至少有上万条记录,我开始找原因:

先输出测试看看格式什么的对不对:

$ for i in `cat file1.log`; do echo "AAA"$i"BBB"; done
BBBxxxxxxxxxxx05_ok
BBBxxxxxxxxxxx06_ok
BBBxxxxxxxxxxx07_ok
BBBxxxxxxxxxxx08_ok

调试一下,发现输出的两端的字符,会选择长度较大的放在最左侧(可以自己试试哈),我一直觉得和换行符有关系,实际上是文件格式的问题。

我最终file了一下源文件和自己再终端随便写的test.log

$ file file1.log
file1.log: ASCII text, with CRLF line terminators

$ file test.log
test.log: ASCII text

终于发现端倪,原来是之前的研发给的结果是win下处理的,是windows的某种格式,没有细究,有兴趣的同学自己去研究文件的格式吧。

总之解决问题最重要:

$ dos2unix file1.log file2.log
file1.logdos2unix: converting file tile1.log to UNIX format ...
file2.logdos2unix: converting file tile2.log to UNIX format ...

测试一下:

$ for i in `cat file1.log`; do echo "AAA"$i"BBB"; done
AAAxxxxxxxxxxx05_okBBB
AAAxxxxxxxxxxx06_okBBB
AAAxxxxxxxxxxx07_okBBB
AAAxxxxxxxxxxx08_okBBB

OK,问题解决。

转载于:https://my.oschina.net/china008/blog/215802

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值