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?
To replace ^M characters in vi editor use below
open the text file say t1.txt
Enter command mode by pressing
shift + :
then press keys as mentioned
%s/^M/\r/g
An alternative to
dos2unix
command would be using standard utilities likesed
.For example, dos to unix:
unix to dos:
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 use0x0A
(a line feed). The^M
you're seeing is a visual representation of0x0D
(a carriage return).dos2unix will help with this. You probably also need to adjust the source of the scripts to be 'Unix-friendly'.
Try using dos2unix to strip off the ^M.
od -a $file
is useful to explore those types of question on Linux (similar to dumphex in the above).Sed is even more widely available and can do this kind of thing also if dos2unix is not installed
You could also try tr:
Here are the results: