merging files with CR as EOL ?

Discussion in 'Ruby' started by Une Bévue, Oct 18, 2006.

  1. Une Bévue

    Une Bévue Guest

    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 ?
     
    Une Bévue, Oct 18, 2006
    #1
    1. Advertising

  2. 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
     
    Robert Klemme, Oct 18, 2006
    #2
    1. Advertising

  3. Une Bévue

    Xavier Noria Guest

    On Oct 18, 2006, at 11:40 AM, Une B=E9vue 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 =20
    > file
    > all together in a unique file, those files have CR lines ending and =20=


    > such
    > must be the merged file.


    If you need to work line-by-line instad of slurping them (which would =20=

    be easier and line-ending agnostic), then pass CR as the optional =20
    separator to your line-oriented idiom. For example

    cr_text_file.each("\015") do |cr_line|
    # ...
    end

    -- fxn
     
    Xavier Noria, Oct 18, 2006
    #3
  4. Une Bévue

    Une Bévue Guest

    Robert Klemme <> wrote:

    > 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


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

    thanks !
     
    Une Bévue, Oct 18, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daves

    replacing EOL with <br>

    Daves, Apr 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    521
    Karl Seguin
    Apr 14, 2005
  2. Tim Tyler
    Replies:
    7
    Views:
    23,005
    danielson317
    Sep 15, 2011
  3. Xah Lee

    replacing two EOL chars by one

    Xah Lee, Dec 20, 2003, in forum: Python
    Replies:
    20
    Views:
    685
  4. Ajay
    Replies:
    0
    Views:
    538
  5. BlueBird
    Replies:
    4
    Views:
    303
    Gabriel Genellina
    Sep 17, 2007
Loading...

Share This Page