由于Windows与Linux对换行的定义不同,导致Windows应用不能正常按行显示Linux文本,Linux显示Windows文本时会带有^M
解决方法#
下面整理一些常用的工具和处理方法:
ftp :#
服务端支持ASCII模式,使用ftp命令可以实现文本换行之间的转换。(由于ASCII存在一些安全隐患,所以部分ftp服务默认关闭ASCII模式传输)。
vi/vim:#
:%s/^M//g
^M: ctrl+v, ctrl+m
awk:#
1
| awk '{ sub("\r$", ""); print }' windows.txt > linux.txt
|
1
| awk 'sub("$", "\r")' linux.txt > windows.txt
|
tr:#
1
| tr -d '\r' < windows.txt > linux.txt
|
sed:#
1
| $ sed -e 's/$/\r/' linux.txt > windows.txt
|
1
| $ sed -e 's/\r$//' windows.txt > linux.txt
|
perl#
1
| $ perl -pe 's/\r?\n|\r/\r\n/g' test.txt > windows.txt
|
1
| $ perl -pe 's/\r?\n|\r/\n/g' test.txt > linux.txt
|
1
| $ perl -pe 's/\r?\n|\r/\r/g' test.txt > mac.txt
|
test.txt可以是windows也可以是linux
相关其他命令#
file#
1
2
3
4
5
6
| $ file windows.txt
windows.txt: ASCII text, with CRLF line terminators
$ file linux.txt
linux.txt: ASCII text
$ file mac.txt
mac.txt: ASCII text, with CR line terminators
|
1
2
3
4
| $ od -a windows.txt
0000000 t e s t cr nl
$ od -a linux.txt
0000000 t e s t nl
|
cat#
1
2
3
4
| $ cat -e windows.txt
test^M$
$ cat -e linux.txt
test$
|
hexdump#
1
2
3
4
| $ hexdump -c windows.txt
0000000 t e s t \r \n
$ hexdump -c linux.txt
0000000 t e s t \n
|