How to Remove Carriage Returns with Ruby

How do I remove carriage returns with Ruby?

What do you get when you do puts lines? That will give you a clue.

By default File.open opens the file in text mode, so your \r\n characters will be automatically converted to \n. Maybe that's the reason lines are always equal to lines2. To prevent Ruby from parsing the line ends use the rb mode:

C:\> copy con lala.txt
a
file
with
many
lines
^Z

C:\> irb
irb(main):001:0> text = File.open('lala.txt').read
=> "a\nfile\nwith\nmany\nlines\n"
irb(main):002:0> bin = File.open('lala.txt', 'rb').read
=> "a\r\nfile\r\nwith\r\nmany\r\nlines\r\n"
irb(main):003:0>

But from your question and code I see you simply need to open the file with the default modifier. You don't need any conversion and may use the shorter File.read.

Ruby: Remove new lines, carriage returns from text

The most efficient way to perform this operation is with String#delete (or #delete!):

text.delete!("\r\n\\")
p text
puts
puts text

Output:

"An Ox came down to a reedy pool to drink. As he splashed heavily into
the water, he crushed a young Frog into the mud.The old Frog soon
missed the little one and asked his brothers and sisters what had
become of him.\"A great big monster,\" said one of them, \"stepped on
little brother with one of his huge feet!\"\"Big, was he!\" said the
old Frog, puffing herself up. \"Was he as big as this?\"\"Oh, much
bigger!\" they cried.The Frog puffed up still more.\"He could not have
been bigger than this,\" she said. But the little Frogs all declared
that the monster was much, much bigger and the old Frog kept puffing
herself out more and more until, all at once, she burst."

An Ox came
down to a reedy pool to drink. As he splashed heavily into the water,
he crushed a young Frog into the mud.The old Frog soon missed the
little one and asked his brothers and sisters what had become of
him."A great big monster," said one of them, "stepped on little
brother with one of his huge feet!""Big, was he!" said the old Frog,
puffing herself up. "Was he as big as this?""Oh, much bigger!" they
cried.The Frog puffed up still more."He could not have been bigger
than this," she said. But the little Frogs all declared that the
monster was much, much bigger and the old Frog kept puffing herself
out more and more until, all at once, she burst.

Benchmark results:

Warming up --------------------------------------
String#gsub 2.826k i/100ms
String#tr 35.794k i/100ms
String#delete 37.147k i/100ms
Calculating -------------------------------------
String#gsub 29.801k (± 2.8%) i/s - 149.778k in 5.030044s
String#tr 399.391k (± 3.3%) i/s - 2.004M in 5.024297s
String#delete 411.065k (± 4.0%) i/s - 2.080M in 5.068783s

I used /\R+|\// for the String#gsub method.

How do I remove carriage returns with Ruby without replacing it with - or blank space

This should do the trick:

"LIC_001  \r\n      ,\r\n        LIC_002  \r\n      ".squish
#=> "LIC_001 , LIC_002"

How can I remove the string \n from within a Ruby string?

You need to use "\n" not '\n' in your gsub. The different quote marks behave differently.

Double quotes " allow character expansion and expression interpolation ie. they let you use escaped control chars like \n to represent their true value, in this case, newline, and allow the use of #{expression} so you can weave variables and, well, pretty much any ruby expression you like into the text.

While on the other hand, single quotes ' treat the string literally, so there's no expansion, replacement, interpolation or what have you.

In this particular case, it's better to use either the .delete or .tr String method to delete the newlines.

See here for more info

Removing line breaks in Ruby

Single-quoted strings do not process most escape sequences. So, when you have this

'\n'

it literally means "two character string, where first character is backslash and second character is lower-case 'n'". It does not mean "newline character". In order for \n to mean newline char, you have to put it inside of double-quoted string (which does process this escape sequence). Here are a few examples:

"Remove \n".delete('\n') # => "Remove \n" # doesn't match
'Remove \n'.delete('\n') # => "Remove \\" # see below

'Remove \n'.delete("\n") # => "Remove \\n" # no newline in source string
"Remove \n".delete("\n") # => "Remove " # properly removed

NOTE that backslash character in this particular example (second line, using single-quoted string in delete call) is simply ignored, because of special logic in the delete method. See doc on String#count for more info. To bypass this, use gsub, for example

'Remove \n'.gsub('\n', '') # => "Remove "

How to remove carriage returns from string but leave line feeds?

Use the destructive version of the method to actually modify the variable string.

string.gsub! "\r", ""

How to stop Ruby automatically adding carriage returns

Opening the file in binary mode causes Ruby to 'suppress EOL <-> CRLF conversion on Windows' (see here).

The problem is that Tempfiles are automatically opened in w+ mode. I couldn't find a way to change this on creation of the Tempfile.

The answer is to change it after creation using binmode:

def test

my_file = Tempfile.new('filetemp.txt')
my_file.binmode

my_file.print "This is on the first line"
my_file.print "\x0A" # \n now also works as a newline character
my_file.print "This is on the second line"

my_file.close

FileUtils.mv(my_file.path, "C:/Users/me/Desktop/Folder/test.usr")

end

Removing carriage returns from CSV columns using converters in Ruby

So, the issue is that you want to convert the output to a file and not the input. The :converters option only handles converting input from a CSV file, it doesn't apply the conversions when writing out in CSV format.

If you really want this functionality you can monkey patch it in:

require 'csv'

class CSV
alias :old_init_separators :init_separators

def init_separators(options)
old_init_separators(options)

if options.delete(:remove_newlines)
old_quote = @quote
@quote = lambda do |field|
old_quote.call(String(field).tr("\r\n", ""))
end
end
end
end

CSV.open('test.csv', 'wb',
:force_quotes => true,
:col_sep => '|',
:remove_newlines => true) do |csv|

csv << ["A\r\nB", "C\r\nD"]
csv << ["E\r\nF", "G\r\nH"]

end

Note the addition of the :remove_newlines option.

$ cat test.csv
"AB"|"CD"
"EF"|"GH"

How to remove line break when reading files in Ruby

Your code has a minor issue that causes the results you are experiencing.

when you use:

name1 = File.readlines('first.txt').sample(1)

The returned value ISN'T a String, but rather an Array with 1 random sample. i.e:

["Jhon"]

This is why you get the output ["Jhon"] when using print.

Since you expect (and prefer) a string, try this instead:

name1 = File.readlines('first.txt').sample(1)[0]
name2 = File.readlines('middle.txt').sample(1)[0]
name3 = File.readlines('last.txt').sample(1)[0]

or:

name1 = File.readlines('first.txt').sample(1).pop
name2 = File.readlines('middle.txt').sample(1).pop
name3 = File.readlines('last.txt').sample(1).pop

or, probably what you meant, with no arguments, sample will return an object instead of an Array:

name1 = File.readlines('first.txt').sample
name2 = File.readlines('middle.txt').sample
name3 = File.readlines('last.txt').sample

Also, while printing, it would be better if you created one string to include all the spaces and formatting you wanted. i.e.:

name1 = File.readlines('first.txt').sample(1).pop
name2 = File.readlines('middle.txt').sample(1).pop
name3 = File.readlines('last.txt').sample(1).pop

puts "#{name1} #{name2} #{name3}."
# or
print "#{name1} #{name2} #{name3}."


Related Topics



Leave a reply



Submit