Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write DOS line endings to a file from Unix

My Unix Ruby program needs to write a file that will be read by SqlServer running on Windows. I need for the lines I write to this file to end in \r\n, the DOS/Windows line ending, rather than \n, the Unix line ending. I want this to happen without me having to manually add the \r to the end of each line.

The starting point

If my program writes to the file like this:

File.open("/tmp/foo", "w") do |file|
  file.puts "stuff"
end

Then the file has Unix line endings:

$ od -c foo
0000000   s   t   u   f   f  \n

That is expected, since my program is running on Unix. But I need for this file (and this file only) to have DOS line endings.

Adding the \r to each line manually

If I add the \r to each line manually:

File.open("/tmp/foo", "w") do |file|
  file.puts "stuff\r"
end

Then the file has DOS line endings:

$ od -c /tmp/foo
0000000   s   t   u   f   f  \r  \n

This works, but has to be repeated for each line I want to write.

Using String#encode

As shown by this SO answer, I can modify the string using String#encode before writing it:

File.open("/tmp/foo", "w") do |file|
  file.puts "alpha\nbeta\n".encode(crlf_newline: true)
end

This results in DOS line endings:

$ od -c /tmp/foo
0000000   a   l   p   h   a  \r  \n   b   e   t   a  \r  \n

This has the advantage that if I am writing multiple lines at once, one call to #encode will change all the line endings for that one write. However, it's verbose, and I still have to specify the line ending for every write.

How can I cause each puts to an open file in Unix to end the line in the Windows \r\n line ending rather than the Unix '\n' line ending?

I am running Ruby 2.3.1.

like image 854
Wayne Conrad Avatar asked May 09 '16 16:05

Wayne Conrad


People also ask

How do you change DOS line ends in Unix?

To write your file in this way, while you have the file open, go to the Edit menu, select the "EOL Conversion" submenu, and from the options that come up select "UNIX/OSX Format". The next time you save the file, its line endings will, all going well, be saved with UNIX-style line endings.

What line endings do you use Unix?

DOS uses carriage return and line feed ("\r\n") as a line ending, which Unix uses just line feed ("\n"). You need to be careful about transferring files between Windows machines and Unix machines to make sure the line endings are translated properly.


1 Answers

There's an option for this, the same one you're using on encode also works with File.open:

File.open('/tmp/foo', mode: 'w', crlf_newline: true) do |file|
  file.puts("alpha")
  file.puts("beta")
end

This option not only ends lines in \r\n, it also translates any explicit \n into \r\n. So this:

file.puts("alpha\nbar")

will write alpha\r\nbar\r\b

like image 97
tadman Avatar answered Sep 17 '22 16:09

tadman