Search code examples
sedawk

remove ^M character from file using sed


I have this line inside a file:

ULNET-PA,client_sgcib,broker_keplersecurities
,KEPLER

I want to get rid of the carriage return character, ^M. I've tried:

sed 's/^M//g'

However, this removes ^M and everything after:

[root@localhost tmp]# vi test
ULNET-PA,client_sgcib,broker_keplersecurities^M,KEPLER

[root@localhost tmp]# sed 's/^M//g' test
ULNET-PA,client_sgcib,broker_keplersecurities

What I want is:

[root@localhost tmp]# vi test
ULNET-PA,client_sgcib,broker_keplersecurities,KEPLER

Solution

  • 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