Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove ^M from text file

Tags:

delimiter

awk

I have text file which shows ^M character when opened using less command in mac terminal. I tried using the below command to remove ^M character.

awk '{ gsub("\n", "\r"); print $0;}' input > output
cat input | tr ‘\n’ ‘\r’ > output

But none of them worked. Could someone help to fix this using some Linux commands.

like image 463
chas Avatar asked Feb 25 '14 19:02

chas


People also ask

What is M in text files?

What is this ^M? The ^M is a carriage-return character. If you see this, you're probably looking at a file that originated in the DOS/Windows world, where an end-of-line is marked by a carriage return/newline pair, whereas in the Unix world, end-of-line is marked by a single newline.

What character is CTRL-M?

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 you get control M characters?

To enter ^M, type CTRL-V, then CTRL-M i.e. hold down the CTRL key then press V and M in succession. where "RET" means and C-q and C-m mean . See Practical Guide to Linux Commands, Editors, and Shell Programming 3rd Edition by Mark G.


2 Answers

You can use sed:

 sed 's/^M// filename > newfilename

If you wish to use awk then do:

awk '{sub(/^M/,"")}1' filename > newfilename

To enter ^M, type CTRL-V, then CTRL-M. That is, hold down the CTRL key then press V and M in succession.

Update

As suggested by @glenn jackman in comments, it is easy to use \r then to get ^M

like image 139
jaypal singh Avatar answered Nov 08 '22 23:11

jaypal singh


col < input > output

Or:

vim "+set ff=unix" "+saveas output" "+q" input
like image 20
Jonathan Wakely Avatar answered Nov 09 '22 00:11

Jonathan Wakely