# ^M^M Control Character Substitution

**URL:** <https://www.ruby-forum.com/t/m-m-control-character-substitution/177308>\
**Category:** Ruby\
**Created:** [October 14, 2009, 7:12pm UTC](https://www.ruby-forum.com/t/m-m-control-character-substitution/177308 "2009-10-14T19:12:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodfriend](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@goodfriend](https://www.ruby-forum.com/u/goodfriend)\
**Post date:** [October 14, 2009, 7:12pm UTC](https://www.ruby-forum.com/t/m-m-control-character-substitution/177308/1 "2009-10-14T19:12:10Z")

</div>

Hi,

I have code that successfully reads a CSV file using FasterCSV and puts  
the data into a simple, 2x2 array.

Some of the cells I read in and print out to a file contain some kind of  
a control character that I don’t know how to strip out and substitute  
with formal line feeds or carriage returns.

Data in original spreadsheet cell that gets read in as CSV reads:

"…

1. String A
2. String B

…"

Data, after reading CSV file and printing out cell to a file reads:

"…

1. String A^M^MString B

…"

My problem is that I need to get the latter output to look like the  
original data.

Does anyone know a quick and simple way to parse the string for the  
control character “^M^M” and replace it with a formal line feed or  
carriage return?

Thanks for your help,

Frank

---

<div class="post-metadata">

**Author:** ![goodfriend](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4da419/32.png) [@goodfriend](https://www.ruby-forum.com/u/goodfriend)\
**Post date:** [October 14, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/m-m-control-character-substitution/177308/2 "2009-10-14T23:05:44Z")

</div>

Hi!

Frank G. schrieb:

> Data, after reading CSV file and printing out cell to a file reads:
> 
> "…
> 
> 1. String A^M^MString B  
> …"
> 
> My problem is that I need to get the latter output to look like the  
> original data.  
> […]

The “^M” should represent a “carridge return” control character.  
The CR of a CRLF linebreak, see [1]. In a ruby regular  
expression, ^M should be matched by \r.

[1] [Newline - Wikipedia](http://en.wikipedia.org/wiki/Newline)

It seems like different or wrong types of newlines/linebreaks  
inside the CSV file (mixed CRLF and LF, …).

Since I don’t know FasterCSV, there may be a solution for this  
within FasterCSV.

If not, replacing ^M^M with a single newline might help:

data.gsub(/\r\r/, “\n”)

Regards,  
Daniel
