2

I have following lines with ^M (return carriage). I want to remove ^M+proceeding line. You can notice there is a new line after each ^M, which is not desired. I purpose is to remove control character and make one line CSV.

    '1.0';'';'';'';'Mon Jul 04 00:00:00 CEST 2016';'To cash Pls Acc 4142^M
To cash Pls Acc 4142';'To cash Pls Acc 4142^M
Money';'236159';'236159';'-2000.0';'';'2000.0'

I tried following but didn't work. NOTE: ^M = CONTROL v + CONTRL m

cat file.csv | sed 's/[\^M\n]//g' > new_file.csv

above lines removed ^M but the line is still broken with new line.

expected output

 '1.0';'';'';'';'Mon Jul 04 00:00:00 CEST 2016';'To cash Pls Acc 4142To cash Pls Acc 4142';'To cash Pls Acc 4142 Money';'236159';'236159';'-2000.0';'';'2000.0'
amjad
  • 121

2 Answers2

3

This works for your case:

sed -i ':a;N;$!ba;s/\r\n//g' file.csv

From: Can sed replace new line characters?

Or this one:

sed -e :a -e '/\r$/N; s/\r\n//; ta' file.csv

From: http://www.catonmat.net/blog/sed-one-liners-explained-part-one/

Note that you don't need to use cat, and you can directly edit your file with the option -i if you want.

andreatsh
  • 2,025
  • I am still getting output file ^M after running above. sed -i ':a;N;$!ba;s/^M\n//g' file.csv > file_without_m.csv. – amjad Jan 02 '17 at 12:55
  • Try replacing \^M with \r – steeldriver Jan 02 '17 at 13:33
  • You're right, I edited my answer. But remember: use the -i option only if you want to directly modify file.csv. If you just want to redirect output to file_without_m.csv you don't need -i. – andreatsh Jan 02 '17 at 18:56
-1

It's \r not ^M. The simplest way to do it is:

tr -d '\n-\r' < file.

-d means delete and \n-\r is the character range for truncation.

Note that this code will also remove occurrences of \ns not preceded by "^M"s.

argle
  • 523
  • About the downvoting. Without an explanation, my only suspicion is that you found my answer redundant. I think the simplest method is a good answer to add. – argle Jan 03 '17 at 09:32