linux文件夹后面有m,linux 文件 ^M 导致的异常

给当前文件最后一列的最后添加一个字符,发现跑到行首去了。并从行首开始继续处理。

[root@VM_1_13_centos ~]# cat t1

core,SIT,rh,10101

[root@VM_1_13_centos ~]# cat t1 |awk -F, '{print $1,$2,$3,$4}'

core SIT rh 10101

[root@VM_1_13_centos ~]# cat t1 |awk -F, '{print $1,$2,$3,$4"\""}'

"ore SIT rh 10101

在linux创建一个文件a1,手动输入内容与t1同。使用cat -A对比。如下:

[root@VM_1_13_centos ~]# cat a1

core,SIT,rh,10101

[root@VM_1_13_centos ~]# cat t1

core,SIT,rh,10101

[root@VM_1_13_centos ~]# cat -A a1

core,SIT,rh,10101$

[root@VM_1_13_centos ~]# cat -A t1

core,SIT,rh,10101^M$

可以看到,t1和a1的内容是不同的,多了特殊字符 ^M。

^M 是什么?

在unix操作系统下的换行符格式为0A(ascii码),而dos格式下的换行符为 0D 0A(也就是),其实就显示为^M了。

去除 ^M 办法

1. dos2unix

[root@VM_1_13_centos ~]# yum install dos2unix -y

[root@VM_1_13_centos ~]# dos2unix t1

dos2unix: converting file t1 to Unix format ...

[root@VM_1_13_centos ~]# cat -A t1

core,SIT,rh,10101$

2. sed

[root@VM_1_13_centos ~]# cat -A t1

core,SIT,rh,10101^M$

#注意,这里^M的输入方法是先按 Ctrl + v 然后按 Ctrl +m

[root@VM_1_13_centos ~]# sed -i 's/^M//' t1

[root@VM_1_13_centos ~]# cat -A t1

core,SIT,rh,10101$

注意,这里^M的输入方法是先按 Ctrl + v 然后按 Ctrl +m

3. vi

:1,$ s/^M//g

^M 输入方法: ctrl+V ,ctrl+M

注意,这里^M的输入方法是先按 Ctrl + v 然后按 Ctrl +m

(没验证成功,汗颜)

:set fileformat=unix

(验证成功^^)

4. tr

[root@VM_1_13_centos ~]# cat t1 | tr -d '\r' >t1g

[root@VM_1_13_centos ~]#

[root@VM_1_13_centos ~]#

[root@VM_1_13_centos ~]# cat t1

core,SIT,rh,10101

[root@VM_1_13_centos ~]# cat t1g

core,SIT,rh,10101

[root@VM_1_13_centos ~]# cat -A t1g

core,SIT,rh,10101$

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值