Symptom
If there is a batch file in Dos/Windows, and if it is copied to Unix/Linux and run it as a shell file, it appends %0D at the end, and the command won't work. But, if the file is opened in a text editor, everything looks proper and you cannot find any ^M characters.
Cause:
In DOS/Windows, the new line is represented by CRLF. In Linux, it is represented by LF, and in Mac, it is represented by CR. If you are getting error with 0x0D in Unix/Linux, it is due to the difference in the line terminator only. The hexadecimal code for CR is 0x0D and it is represented by ^M in VIM and other similar text editors.
Verifying:
Run hexdump on that file, and see whether you are seeing 0a0d or only 0a. If you see 0a0d, it means, it is a DOS file.
Resolution:
Easiest solution is, if you have dos2unix command, then just running that command on that file would remove the 0x0D characters.
You can remove it from VIM also. If you open that file in VIM, and if you see ^M characters, then run the command
:%s/<Ctrl+V><Enter>//g
The above command will remove all the ^M characters in the file, and the file can be used as if, it is a unix/linux file.
If you don't see ^M characters in the file, it may be because, the configuration has been set to recognize DOS files. (i.e., you will not see ^M characters, which are valid in DOS). To remove the ^M characters, first we have to let VIM to show them. For that, we need to set the file format as unix, and reload the file to open in that format. The following commands set the file format to unix and reloads the file.
:set ffs=unix
:e <filename>
If we run the above two commands, it will show ^M characters, and by running the command (:%s/<Ctrl+V><Enter>//g), those characters would be removed, and you can use that file as if, it is unix/linux file. The above commands have the scope of the present buffer, and if we reopen VIM, the file format would be reverted to the one that is set in the configuration file.
Thursday, December 09, 2010
Error With %0D in Linux - Removing 0D characters in VIM
Labels:
technical
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment