行尾的'^ M'字符 ('^M' character at end of lines)


問題描述

'^M' 字符在行尾 ('^M' character at end of lines)

當我在 Unix 環境中運行特定的 SQL 腳本時,我在 SQL 腳本的每一行末尾看到一個“^M”字符,因為它回顯到命令行。我不知道 SQL 腳本最初是在哪個操作系統上創建的。

這是什麼原因造成的,我該如何解決?


參考解法

方法 1:

It's caused by the DOS/Windows line‑ending characters. Like Andy Whitfield said, the Unix command dos2unix will help fix the problem. If you want more information, you can read the man pages for that command.

方法 2:

Fix line endings in vi by running the following:

:set fileformat=unix

:w

方法 3:

The cause is the difference between how a Windows‑based based OS and a Unix based OS store the end‑of‑line markers.

Windows based operating systems, thanks to their DOS heritage, store an end‑of‑line as a pair of characters ‑ 0x0D0A (carriage return + line feed). Unix‑based operating systems just use 0x0A (a line feed). The ^M you're seeing is a visual representation of 0x0D (a carriage return).

dos2unix will help with this. You probably also need to adjust the source of the scripts to be 'Unix‑friendly'.

方法 4:

The easiest way is to use vi. I know that sounds terrible but its simple and already installed on most UNIX environments. The ^M is a new line from Windows/DOS environment.

from the command prompt: $ vi filename

Then press ":" to get to command mode.

Search and Replace all Globally is :%s/^M//g "Press and hold control then press V then M" which will replace ^M with nothing.

Then to write and quit enter ":wq" Done!

方法 5:

Try using dos2unix to strip off the ^M.

(by Paul ReinersThomas OwensTim AbellColinYoungerBernie PerezAndy Whitfield)

參考文件

  1. '^M' character at end of lines (CC BY‑SA 2.5/3.0/4.0)

#newline #SQL #unix #os-dependent #line-endings






相關問題

如何格式化電子郵件中的字符串以便 Outlook 打印換行符? (How do I format a String in an email so Outlook will print the line breaks?)

contentEditable поле для захавання новых радкоў пры ўваходзе ў базу дадзеных (contentEditable field to maintain newlines upon database entry)

換行符 (newline character(s))

grep 如何匹配一些字母或行尾 (grep how to match some letters OR end of line)

Trong Python, có thể thoát các ký tự dòng mới khi in một chuỗi không? (In Python, is it possible to escape newline characters when printing a string?)

在 SWIFT 中遇到逗號時將字符串換行 (Break string to newline when meeting a comma in SWIFT)

行尾的'^ M'字符 ('^M' character at end of lines)

從C#中的字符串末尾刪除回車符和換行符 (Removing carriage return and new-line from the end of a string in c#)

新線路和瀏覽器/操作系統兼容性 (New lines and browser/OS compatability)

如何使用 grep 查找單詞後兩個字符之間的所有內容,而不輸出整行? (How do I find everything between two characters after a word using grep, without outputting the entire line?)

在 Perl 中寫入文件的問題 (Issues writing to a file in Perl)

在 for 循環中每 7 行換行一次 (Newline every 7 lines within a for loop)







留言討論