不同系統文本文件的行尾換行符不同:
Windows為一個回車’\r’(CR或^M)和一個換行’\n’(NL或LF)(括號內是其它顯示方法)
Linux為一個換行’\n’
Mac為一個回車’\r’
查看文件是否含有Windows換行符:
Windows:Notepad++ ==>視圖 ==>顯示所有符號
Linux:file test.txt
test.txt: ASCII text, with CRLF line terminators
Vim:命令模式下輸入:e ++ff=unix,^M就是Windows換行符
轉換方法:
Windows下Notepad++ ==>編輯 ==> 文檔格式轉換 ==> 轉為Unix
Linux:sed -i 's/\r//' filename
Linux:dos2unix filename(需要先安裝dos2unix)
dos2unix語法:
dos2unix [-hkqV] [-c convmode] [-o file …] [-n infile outfile …]
參數說明:
-k:保持輸出文件的日期不變
-q:安靜模式,不提示任何警告信息。
-V:查看版本
-c:轉換模式,模式有:ASCII, 7bit, ISO, Mac, 默認是:ASCII。
-o:寫入到源文件
-n:寫入到新文件
Linux批量轉換:find -type f | xargs dos2unix -o
Vim:命令模式下輸入:%s/^M//g或者:g/\^M/s/\^M//
Vim:命令模式輸入:set ff?如果出現fileforma=dos 表示是Windows上的換行符。繼續輸入:set fileformat=unix 保存即可