資訊人筆記

Work hard, Have fun, Make history!

使用者工具

網站工具


operating_system:freebsd_unix_dos文字檔案格式轉換

[FreeBSD] UNIX,Dos文字檔案格式轉換

0x00 前言

文字檔在windows與FreeBSD上使用時常會遇到換行表示符號的問題

這個問題是因為不同的系統所使用的換行字元不同所引起的,Windows 中的換行字元是 \n\r,Linux 是 \n,Mac 則是 \r

本篇記錄此問題的簡易處理方式


0x01 檢查檔案

透過 cat -v 顯示non-printing characters,確認檔案中是否包含一些多餘字元,如果是換行問題可以看到 ^M 出現

直接vim不一定會出現 ^M

cat -v filename

透過 file 指令確認file type

file filename

Dos格式的檔案會出現with CRLF line terminators

NTtest.sh: POSIX shell script, ASCII text executable
NTtest1.sh: POSIX shell script, ASCII text executable, with CRLF line terminators

Windows text files tend to have carriage-return line-feed characters at the end of each line. CRLF.

Linux text files have a line-feed character at the end of the line. LF.


0x02 處理方式

若是單檔,可以在vim中使用

:set ff=unix

存檔後在用cat -v 或file就可以看到轉成unix格式了

若需要批次轉換則可使用tofrodos


0x03 編碼問題

另外在 windows 和 mac 間也會出現檔案編碼不一致,導致中文無法顯示的狀況

若原來為 big5 (ISO-8859) 編碼,vim 開啟檔案後可用 :write ++enc=utf-8 newfile.txt

另存新檔為 UTF8 編碼

file 指令看到檔案為 ASCII 編碼,由於 ASCII 為 UTF8 子集,編碼方式本來就相同,即使做了轉換也不會改變,所以直接使用就行了


0x04 參考資料

operating_system/freebsd_unix_dos文字檔案格式轉換.txt · 上一次變更: 127.0.0.1