因为windows下文本的换行符为\r\n,而unix下只为\n,因此windows换行符(\r\n)与unix换行符(\n)互转,最直观的做法是直接用sed替换。
windows换行符转unix换行符:
sed -i -e 's/\r\n/\n/g' test.txt
unix换行符转windows换行符:
sed -i -e 's/\n/\r\n/g' test.txt
或者使用删除\r字符的思路,将windows换行符转换成unix换行符
sed -i 's/^M//g' test.txt
上面命令中,符号^M即为 \r ,即把\r都删除掉
或者更直观点的做法为:
sed -i -e 's/\r//g' test.txt
linux下想要查看文件的换行符,其中一种方法可以使用cat -A filename 命令:
[root@localhost ~]# cat -A test.txt
this is a new line test^M$
this is a new line test1^M$
\r对应符号^M,\n对应符号$.