Linux 与 Windows 文本文件格式转换

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

2019-09-01 · 1 min · 157 words