16

In a C# module, I want to transfer files from Windows to Unix using Tectia. But the problem is when these files are transferred(Ascii or Binary mode both) and opened using VI editor we get ^M characters. I searched about this but the solutions are to remove these ^M characters after the files are transferred using utilites. Is there any way that these ^M characters do not appear in the first place. Is there any option to have a workaround in code before sending these files?

10 Answers 10

25

How I was able to remove it in vi editor:

  • After :%s/ then press ctrl+V then ctrl+M. This will give you ^M
  • Then //g (will look like: :%s/^M ) press Enter should get all removed.

Good luck!

2
  • 1
    this key-by-key input is absolutely necessary as copy and paste won't do the trick. Nov 8, 2014 at 21:36
  • This just doesn't work if you're using vi from a terminal in windows because the control characters don't work the same way. The dos2unix solution is simplest if you have the ability to install it (I added it to my docker dev environment) May 5, 2017 at 9:53
17

If you just need to remove the ^M characters (not replace them with \n):

sed -i -e 's/\r//g' yourfile.txt

If you want to replace them with \n:

sed -i -e 's/\r/\n/g' yourfile.txt
14

You can install and use dos2unix. After Installation just run:

>dos2unix yourfile.txt 
5
  • hi...I know about dos2unix utility but Is there any way that these ctrl M characters do not appear only? Sep 20, 2012 at 7:49
  • Unix uses a single Line Feed (LF) character as line break. Windows/DOS uses 2 characters: Carriage Return/Line Feed (CR/LF). So the answer is no, you cannot eliminate the CR\LF (unless you write all your text in one line).
    – Nir Alfasi
    Sep 20, 2012 at 7:54
  • @RaviGoklani I'm not aware of a windows tool that does such conversions, but since you use vim this article might interest you.
    – Nir Alfasi
    Sep 21, 2012 at 17:39
  • this worked for me without the >, only; i.e. dos2unix yourfile.txt
    – floer32
    Aug 6, 2013 at 19:15
  • 1
    The > is just the prompt sign ;)
    – Nir Alfasi
    Aug 6, 2013 at 19:16
5

One more trick to remove Ctrl+M in vi editor:

:%s/^V^M//g

For more trick to remove Ctrl+M characters

3

Try executing the following in your terminal (may need to install it first):

fromdos <your-file>
2
  • actually I dont want to run anything on UNIX after transfer is done..no command,no utility etc....just my file should be transferred from Windows to Unix without those ^M characters Sep 20, 2012 at 11:24
  • Then you need to make sure you set the file to UNIX file format before sending your file over. I believe it's the EOL (line endings) that are causing it, any decent text editor will allow you to change the line ending type. Sep 21, 2012 at 10:32
1
:%s/^V^M//
  1. g not needed at all.

  2. %s - find and replace text that u want to replace

  3. ^V^M to be replaced with "should be after double slash" keep it empty if you want to replace with nothing

1

Try

tr -d '\015' <INPUT_FILE > OUTPUT_FILE
1
  • tr -d '\015' <INPUT_FILE > OUTPUT_FILE
    – Akshay
    Jun 30, 2015 at 8:57
0

tr '\015' '\n' < /tmp/file-with-ctrlm.txt > /tmp/no-ctrlm.txt

  • This command uses ASCII code of CtrlM and replaces all CtrlM with newline characters.
  • It avoids Ctrl-V, Ctrl-M, and works on Mac too.
0

For me this is the only thing that would work .. not unix2dos or anything ...

sed -e 's/\x0D//g'

reference:

The hexadecimal 0a, a control character as opposed to a printing character, is called a line feed.

The hexadecimal 0d is called a carriage return.

0

Thanks for all the help everyone. I solved this problem using a workaround. Windows uses CR+LF (\r\n) as end-of-line & Unix uses LF (\n) as end-of-line.I took the Windows file and replaced the occurrence of CR+LF(\r\n) with LF(\n) in code itself without any utility.This made the file compatible for Unix systems and then I transferred the file using SFTP and it worked on Unix without ^M characters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.