Replacing parts of newlines

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs