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?
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 ...]
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.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.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!You can remove ^M from the files directly via sed command, e.g.:
If you're happy with the changes, remove the .bak files:
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.Fix line endings in
vi
by running the following::set fileformat=unix
:w