chas
chas

Reputation: 1645

Remove ^M from text file

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.

Upvotes: 9

Views: 10823

Answers (3)

jaypal singh
jaypal singh

Reputation: 77145

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

Upvotes: 6

Henry Barber
Henry Barber

Reputation: 133

use the octal value from http://www.asciitable.com/

echo "1'2^M34" | awk 'gsub(/\015/,":")' 1'2:34

Upvotes: 1

Jonathan Wakely
Jonathan Wakely

Reputation: 171383

col < input > output

Or:

vim "+set ff=unix" "+saveas output" "+q" input

Upvotes: 3

Related Questions