SQL '^M' 字符在行尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/64749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:22:45  来源:igfitidea点击:

'^M' character at end of lines

sqlunixnewlineline-endingsos-dependent

提问by Paul Reiners

When I run a particular SQL script in Unix environments, I'm am seeing a '^M' character at the end of each line of the SQL script as it is echoed to the command-line. I don't know on which OS the SQL script was originally created.

当我在 Unix 环境中运行特定的 SQL 脚本时,我在 SQL 脚本的每一行末尾看到一个 '^M' 字符,因为它被回显到命令行。我不知道 SQL 脚本最初是在哪个操作系统上创建的。

What is causing this and how do I fix it?

这是什么原因造成的,我该如何解决?

采纳答案by Thomas Owens

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.

它是由 DOS/Windows 行结束字符引起的。就像 Andy Whitfield 所说的,Unix 命令 dos2unix 将有助于解决这个问题。如果需要更多信息,可以阅读该命令的手册页。

回答by Tim Abell

Fix line endings in viby running the following:

vi通过运行以下命令修复行结尾:

:set fileformat=unix

:set fileformat=unix

:w

:w

回答by ColinYounger

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

原因是基于 Windows 的操作系统和基于 Unix 的操作系统存储行尾标记的方式不同。

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 ^Myou're seeing is a visual representation of 0x0D(a carriage return).

基于 Windows 的操作系统,由于它们的 DOS 传统,将行尾存储为一对字符 - 0x0D0A(回车 + 换行)。基于 Unix 的操作系统只使用0x0A换行)。在^M你看到的是一个可视化表示0x0D(一个回车)。

dos2unixwill help with this. You probably also need to adjust the source of the scripts to be 'Unix-friendly'.

dos2unix将对此有所帮助。您可能还需要将脚本源调整为“Unix 友好”。

回答by Bernie Perez

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

最简单的方法是使用vi. 我知道这听起来很糟糕,但它很简单并且已经安装在大多数 UNIX 环境中。^M 是来自 Windows/DOS 环境的新行。

from the command prompt: $ vi filename

从命令提示符: $ 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.

全局搜索和替换所有内容是:%s/^M//g按住控制键,然后按 V 然后按 M”,这将用空替换 ^M。

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

然后写入并退出输入“ :wq”完成!

回答by Andy Whitfield

Try using dos2unix to strip off the ^M.

尝试使用 dos2unix 去除 ^M。

回答by dogbane

In vi, do a :%s/^M//g

在 vi 中,做一个 :%s/^M//g

To get the ^Mhold the CTRLkey, press Vthen M(Both while holding the control key) and the ^Mwill appear. This will find all occurrences and replace them with nothing.

要获得^M保持CTRL键,请按Vthen M(同时按住控制键),^M将出现。这将找到所有事件并用空替换它们。

回答by Bill the Lizard

The SQL script was originally created on a Windows OS. The '^M' characters are a result of Windows and Unix having different ideas about what to use for an end-of-line character. You can use perl at the command line to fix this.

SQL 脚本最初是在 Windows 操作系统上创建的。'^M' 字符是 Windows 和 Unix 对行尾字符使用什么有不同想法的结果。您可以在命令行中使用 perl 来解决此问题。

perl -pie 's/\r//g' filename.txt

回答by jW.

The ^M is typically caused by the Windows operator newlines, and translated onto Unix looks like a ^M. The command dos2unix should remove them nicely

^M 通常是由 Windows 操作符换行引起的,转换到 Unix 上看起来像 ^M。命令 dos2unix 应该很好地删除它们

dos2unix [options] [-c convmode] [-o file ...] [-n infile outfile ...]

dos2unix [选项] [-c convmode] [-o 文件 ...] [-n infile outfile ...]

回答by jW.

C:\tmp\text>dos2unix hello.txt helloUNIX.txt

Sed is even more widely available and can do this kind of thing also if dos2unix is not installed

sed 的使用范围更广,如果没有安装 dos2unix 也可以做这种事情

C:\tmp\text>sed s/\r// hello.txt > helloUNIX.txt  

You could also try tr:

你也可以试试 tr:

cat hello.txt | tr -d \r > helloUNIX2.txt  

Here are the results:

结果如下:

C:\tmp\text>dumphex hello.txt  
00000000h: 48 61 68 61 0D 0A 68 61 68 61 0D 0A 68 61 68 61 Haha..haha..haha  
00000010h: 0D 0A 0D 0A 68 61 68 61 0D 0A                   ....haha..  

C:\tmp\text>dumphex helloUNIX.txt  
00000000h: 48 61 68 61 0A 68 61 68 61 0A 68 61 68 61 0A 0A Haha.haha.haha..  
00000010h: 68 61 68 61 0A                                  haha.  

C:\tmp\text>dumphex helloUNIX2.txt  
00000000h: 48 61 68 61 0A 68 61 68 61 0A 68 61 68 61 0A 0A Haha.haha.haha..  
00000010h: 68 61 68 61 0A                                  haha.  

回答by leela

To replace ^M characters in vi editor use below

要在 vi 编辑器中替换 ^M 字符,请使用下面的

open the text file say t1.txt

打开文本文件说 t1.txt

vi t1.txt

Enter command mode by pressing shift + :

按下进入命令模式 shift + :

then press keys as mentioned %s/^M/\r/g

然后按上面提到的键 %s/^M/\r/g

in above ^M is not (shift + 6)M instead it is (ctrl + V)(ctrl + M)