To find ^M (control +M) characters in the file:
For single file: $ grep ^M
filename For Multiple files: $ grep ^M *
(This command searches all the files in the current directory)
- To remove ^M (control +M) characters in the file:
$ dos2unix filename filename
(dos2unix is the command used to delete ^M characters in the file
Find inside vi editor:
Open the file in vi editor mode and type the below command:
For ^V use (Ctrl + v) and for ^M use (Ctrl+m), after you type this, the command looks like this:
This command search for the Control m (^M) character and replace with nothing.
Tips & Warnings
- For transferring files like text files, shell scripts, dat files use ascii mode as it prevents file from getting ^M(control M) characters.
- Press (Ctrl+v) + (Ctrl +m) – (hold Ctrl button and press v and m letter) to type ^M, not (caps+M)
- In some cases, you need to use (Ctrl+v) + (Ctrl+v) + (Ctrl+m) to type ^M character.