# Merging files with CR as EOL?

**URL:** <https://www.ruby-forum.com/t/merging-files-with-cr-as-eol/72700>\
**Category:** Ruby\
**Created:** [October 18, 2006, 11:40am UTC](https://www.ruby-forum.com/t/merging-files-with-cr-as-eol/72700 "2006-10-18T11:40:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 18, 2006, 11:40am UTC](https://www.ruby-forum.com/t/merging-files-with-cr-as-eol/72700/1 "2006-10-18T11:40:44Z")

</div>

i have to rewrite a script in order to be compatible with file lines  
ending by CR instead of unix \n

my script is simple, it take the content of a folder and merge the file  
all together in a unique file, those files have CR lines ending and such  
must be the merged file.

what to do in that case ?

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 18, 2006, 11:46am UTC](https://www.ruby-forum.com/t/merging-files-with-cr-as-eol/72700/2 "2006-10-18T11:46:47Z")

</div>

On 18.10.2006 11:37, Une Bévue wrote:

> i have to rewrite a script in order to be compatible with file lines  
> ending by CR instead of unix \n
> 
> my script is simple, it take the content of a folder and merge the file  
> all together in a unique file, those files have CR lines ending and such  
> must be the merged file.
> 
> what to do in that case ?

You could do something like

# untested

File.open(“result.txt”, “wb”) do |out|  
Dir["\*"].each do |file|  
File.open(file) do |in|  
in.each\_line do |line|  
line.chomp!  
out.write(line)  
out.write("\r\n")  
end  
end  
end  
end

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 18, 2006, 11:52am UTC](https://www.ruby-forum.com/t/merging-files-with-cr-as-eol/72700/3 "2006-10-18T11:52:49Z")

</div>

On Oct 18, 2006, at 11:40 AM, Une Bévue wrote:

> i have to rewrite a script in order to be compatible with file lines  
> ending by CR instead of unix \n
> 
> my script is simple, it take the content of a folder and merge the  
> file  
> all together in a unique file, those files have CR lines ending and  
> such  
> must be the merged file.

If you need to work line-by-line instad of slurping them (which would  
be easier and line-ending agnostic), then pass CR as the optional  
separator to your line-oriented idiom. For example

cr\_text\_file.each("\015") do |cr\_line|  
# …  
end

– fxn

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [October 18, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/merging-files-with-cr-as-eol/72700/4 "2006-10-18T15:42:22Z")

</div>

Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> yes right because within ruby the chomp is aware of the kind of line  
> feed is there !

thanks !
