Reputation: 1105
I have a problem when I try to remove ^M from a csv
When I type vim or vi file.csv, I get
A, TK,2015-04-06,14.4^M,14.7,10.0,0.0,54.0^M,13.3^M,135.0^M,33.8
B, NV,2015-04-06,14.4^M,14.7,5.4,0.0,47.0^M,14.8^M,97.0^M,31.3
I have tried with
tr -d '^M' < file.csv > file2.csv
But it doesn't remove, also with sed.
Upvotes: 13
Views: 16186
Reputation: 107080
Where did you get that file from? It looks like an old System X file from a Mac. The old pre-OSX OS used <CR>
as line endings. Unix uses <LF>
, and Windows/DOS uses <CRLF>
.
Do you have dos2unix. This program can convert line endings from Unix/Linux, DOS/Windows, or System X Macs to any of the formats you want. In your file, I take it you need to convert the ^M
which are Control Ms and not a Caret-M to NL
characters.
Upvotes: 2
Reputation: 3838
You could use dos2unix
command which is provided to do that.
Using GNU/sed just for fun :
sed -i -e "s/\r//g" file
Using tr
:
tr -d '\r' <file1 >file2
Upvotes: 14
Reputation: 63952
You can try:
perl -pE 's/(\^M|\r)//g' < file >file2
should remove
^M
- sequence of two characters ^
and M
^M
as \r
character.Upvotes: 4