'^M' character at end of lines

2019-01-04 09:23发布

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.

What is causing this and how do I fix it?

16条回答
戒情不戒烟
2楼-- · 2019-01-04 09:24

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

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

查看更多
唯我独甜
3楼-- · 2019-01-04 09:25

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

To get the ^M hold the CTRL key, press V then M (Both while holding the control key) and the ^M will appear. This will find all occurrences and replace them with nothing.

查看更多
地球回转人心会变
4楼-- · 2019-01-04 09:27

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!

查看更多
▲ chillily
5楼-- · 2019-01-04 09:27

You can remove ^M from the files directly via sed command, e.g.:

sed -i'.bak' s/\r//g *.*

If you're happy with the changes, remove the .bak files:

rm -v *.bak
查看更多
三岁会撩人
6楼-- · 2019-01-04 09:27

Another vi command that'll do: :%s/.$// This removes the last character of each line in the file. The drawback to this search and replace command is that it doesn't care what the last character is, so be careful not to call it twice.

查看更多
迷人小祖宗
7楼-- · 2019-01-04 09:29

Fix line endings in vi by running the following:

:set fileformat=unix

:w

查看更多
登录 后发表回答