Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell command to strip out ^M characters from text file [duplicate]

Possible Duplicate:
Remove carriage return in Unix

I am reading some data generated by an external third party. I have noticed that the ASCII text in the file is interspersed with ^M characters, which I believe is character 13 in ASCII and represents a carriage return without linefeed.

Is there a one liner I can use to strip the ^M characters from the file?

I am running on Linux (Ubuntu).

like image 499
Homunculus Reticulli Avatar asked Nov 27 '12 17:11

Homunculus Reticulli


People also ask

What is CTRL-M character?

It is known as carriage return. If you're using vim you can enter insert mode and type CTRL - v CTRL - m . That ^M is the keyboard equivalent to \r.

How do I remove special characters from a bash script?

Escape characters are used to remove the special meaning from a single character. A non-quoted backslash, \, is used as an escape character in Bash.

How do I find Control M characters in Unix?

Note: Remember how to type control M characters in UNIX, just hold the control key and then press v and m to get the control-m character.

What is M in Linux file?

Viewing the certificate files in Linux shows ^M characters appended to every line. The file in question was created in Windows and then copied over to Linux. ^M is the keyboard equivalent to \r or CTRL-v + CTRL-m in vim.


2 Answers

You can use sed like this:

sed -i.bak 's/^M$//' infile.txt

To type ^M, you need to type CTRL-V and then CTRL-M.

like image 121
anubhava Avatar answered Oct 11 '22 01:10

anubhava


OR

dos2unix infile.txt file2.txt ....

OR

man dos2unix 

for more details.

like image 37
shellter Avatar answered Oct 10 '22 23:10

shellter