Wondering, why I am writing this post……!!!
It is common problem faced during development. Mostly developer work on Windows platform, create shell script file for ftp, sqlloader, etc and move that file to Unix operating system and define a concurrent program on it.
When you submit the program, it ends in error. DBAs complains that there is Cntrl-M character in the file.
What is this Cntrl-M character? Why are those in the file? How to avoid these?
Let me explain. Unix and Windows use different line termination characters. Unix use only line feed while windows use both carriage return and line feed as termination characters. This CR and LF becomes Cntrl-M in Unix.
Avoid Cntrl-M character
Let me tell you the way to avoid Cntrl-M character from a file.
Text editor option
We use either Notepad++ or Textpad while working on any text file. These editors generally have built-in option to save the file in Unix or Windows format.
In Notepad++, navigate to Edit->EOL Conversion and chose the respective file format.
ASCII FTP Mode
We use sFTP or FTP software like WinSCP to transfer files from Windows to Unix environment. This software provides options to transfer file using either Binary or ASCII mode.
Binary Mode – This mode we use to transfer binary file like form builder file .fmb, report builder file .rdf. It just moves the file as is with all properties.
ASCII Mode – This mode we use to transfer text file. It moves the file from one system to another and it also does explicit character conversion. Windows format file is automatically converted to Unix format file.
Explicit character conversion
Some Unix operating system like Solaris provides built-in command/utility to convert file from Unix to DOS and vice versa.
dos2unix dos_format.txt unix_format.txt or unix2dos unix_format.txt dos_format.txt
These are different methods to avoid Cntrl-M character. I am suggesting to follow approach 1 combined with approach 2.
Want to know more about line termination character, just head on to Wikipedia.