消除Linux上^M換行結尾的三種方式

Louie Wu
Jan 15, 2021

--

因為windows與linux換行字符不同,一般windows檔案上傳到工作站後,檔案打開會是每行結尾都有個^M的換行符號。

主要是因為Linux (LF)換行字元自\n,而window(CRLF)換行字元是\r\n
在Linux開啟window編輯的檔案後會多了一個\r,會顯示成^M
雖無傷大雅但是嫌礙眼的話可以參考以下方式這樣做

此筆記內容皆來自網路搜尋 & stackoverflow

1. dos2unix
Terminal 輸入 dos2unix input.txt
借助工具程式完成

2. Terminal perl one line command
Terminal 輸入 perl -i -pe ‘s/\r$//g’ input.txt
借助perl取代完成

Option flag:
-p: 一行一行輸入進去Places a printing loop around your command so that it acts on each line of standard input. Used mostly so Perl can beat the pants off awk in terms of power AND simplicity :-)

-e: 直接執行command指令Allows you to provide the program as an argument rather than in a file. You don’t want to have to create a script file for every little Perl one-liner.

-i: 取代原本檔案Modifies your input file in-place (making a backup of the original). Handy to modify files without the {copy, delete-original, rename} process.

3. VIM取代
在vim內取代 %s/^M//g
VIM打出^M方式Ctrl+v ctrl+m

--

--

No responses yet