remove ^M characters from file using sed

Use tr:

tr -d '^M' < inputfile

(Note that the ^M character can be input using Ctrl+VCtrl+M)


EDIT: As suggested by Glenn Jackman, if you're using bash, you could also say:

tr -d $'\r' < inputfile

You can simply use dos2unix which is available in most Unix/Linux systems. However I found the following sed command to be better as it removed ^M where dos2unix couldn't:

sed 's/\r//g' < input.txt >  output.txt

Hope that helps.

Note: ^M is actually carriage return character which is represented in code as \r What dos2unix does is most likely equivalent to:

sed 's/\r\n/\n/g' < input.txt >  output.txt

It doesn't remove \r when it is not immediately followed by \n and replaces both with just \n. This fails with certain types of files like one I just tested with.


still the same line:

sed -i 's/^M//g' file

when you type the command, for ^M you type Ctrl+VCtrl+M

actually if you have already opened the file in vim, you can just in vim do:

:%s/^M//g

same, ^M you type Ctrl-V Ctrl-M

Tags:

Awk

Sed