# Replacing parts of newlines

**URL:** <https://www.ruby-forum.com/t/replacing-parts-of-newlines/103173>\
**Category:** Ruby\
**Created:** [May 2, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/replacing-parts-of-newlines/103173 "2007-05-02T18:27:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [May 2, 2007, 6:27pm UTC](https://www.ruby-forum.com/t/replacing-parts-of-newlines/103173/1 "2007-05-02T18:27:48Z")

</div>

I’m writing a script to convert text into a csv file to import into MS  
Excel  
(under Windows), and Excel allows multiline text within a cell, but the  
newlines (\x0D\x0A) have to be replaced with only a \x0A for Excel to  
put  
everything into one cell.

The problem is that in Ruby, if I do:

```
newfield = field.gsub ( "\x0D" , "" )

```

it doesn’t do anything because Ruby treats \x0D\x0A as a unit.

Is there any way around this?

Mike S.

---

<div class="post-metadata">

**Author:** ![mschwab](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mschwab](https://www.ruby-forum.com/u/mschwab)\
**Post date:** [May 2, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/replacing-parts-of-newlines/103173/2 "2007-05-02T18:41:50Z")

</div>

On May 2, 2007, at 6:26 PM, Mike S. wrote:

> newfield = field.gsub ( “\x0D” , “” )
> 
> it doesn’t do anything because Ruby treats \x0D\x0A as a unit.

Not really.

If there’s a CRLF in the text file and you read it using text mode in  
a CRLF platform, the CR is filtered out by the I/O layer. So a line-  
oriented script only sees LFs in its strings, vaguely speaking.  
Reciprocally, when you write to disk in text model in a CRLF platform  
all LFs get a CR automatically prepended. That’s transparent to the  
programmer, in fact that’s done by the very stdio in the C interpreter.

You probably need to work in binmode.

– fxn
