给当前文件最后一列的最后添加一个字符,发现跑到行首去了。并从行首开始继续处理。
[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$