Operating System - Linux
1819688 Members
3486 Online
109605 Solutions
New Discussion юеВ

Why does ^M appears at the end of the line?

 
SOLVED
Go to solution
Henry Chua
Super Advisor

Why does ^M appears at the end of the line?

Hi Guys,

I notice the symbol "^M" appearing at the end of each line in my text file. What does it means? How can I get rid or prevent it from appearing? is there any implication if I remove them?

Best regards
Henry

8 REPLIES 8
Muthukumar_5
Honored Contributor
Solution

Re: Why does ^M appears at the end of the line?

^M is a character will be inserted when moving dos file to unix system. To remove that,

# dos2ux

will do that.

--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Why does ^M appears at the end of the line?

Luk Vandenbussche
Honored Contributor

Re: Why does ^M appears at the end of the line?

Hi,

Did you ftp the file to your server?
Change the transfer mode from ascii to binary to solve this problem

ftp>bin
Peter Godron
Honored Contributor

Re: Why does ^M appears at the end of the line?

Henry,
the ^M is the way the system shows the difference in how UNIX systems and DOS systems interpret the carriage return at the end of the line.
Solutions as to how to remove/prevent the character are aplenty (see earlier answers).
Ninad_1
Honored Contributor

Re: Why does ^M appears at the end of the line?

Luk,

I think the mode needs to be set to ascii when ftping file from DOS to Unix so that the ^M characters are interpreted by Unix as carriage return and do not appear as ^M in the files in UNIX, whereas if you use binary mode the file is ftpied in binary format and Unix does not do any interpretation of any of the characters.
So ascii is the mode required.

Henry,
Next - if you wish to remove the ^M characters from the file you have already got - edit the file using vi, then type the colon - : to go to command mode in vi and give following at colon prompt
s/^M//g
This will remove all the ^M characters .(Note to type the correct ^M character - use the following sequence of keys
Ctrl+V+Shift+M - the Ctrl+V will give you the control character ^ and shit+M will give the M)

Hope this helps.

Ninad
V. Nyga
Honored Contributor

Re: Why does ^M appears at the end of the line?

Sorry Muthu,
it's
dos2ux file.name > new_file.name

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
James R. Ferguson
Acclaimed Contributor

Re: Why does ^M appears at the end of the line?

Hi Henry:

As noted, if you are running on HP-UX you have 'dos2ux' and 'ux2dos'.

The 'dos2ux' and 'ux2dos' commands read each specified file in sequence and write them to standard output, converting from DOS to UNIX format or from UNIX to DOS format, respectively.

If you are *not* on a HP server, you can make strip the carriage returns (^M) with this:

# perl -ni.old -e ' s,\015\012,\012,s; s,\032,,s; file

The above make a backup copy of "file" as "file.old" and performs the conversion.

Regards!

...JRF...
Jeffrey L. Cooke
Occasional Advisor

Re: Why does ^M appears at the end of the line?

^M is the representation of the control character encoded by ASCII code 13. DOS systems use this control code (13) to represent a carriage return.

You generally get this when performing an FTP 'get' or 'put' of a file and you have not selected the proper file transfer mode.

When transferring files from Unix to DOS (Windows) systems and those files are text files, you may wish to manually set the file transfer mode to 'ascii'. That way, the carriage returns and line feed characters will be cleaned up during the FTP transfer.

If you forget (or get confused by all this nonsense), then just use the utility specified in another post (dos2ux) or you can use a vi command, if you are comfortable with that:

[ESC]:%s/[Ctrl-V][Ctrl-M]$//

This command says: for all lines in the file (%), substitute (s), the first pattern (the sequence ctrl-v,ctrl-m is how you get Ctrl-M in vi) -- only when it appears at the end of a record ($), with the second pattern (null).

--Good luck, Jeff