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. Advertisements

  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. Advertisements

  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. Advertisements

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. Joe
    Replies:
    0
    Views:
    508
  2. Nathan Coast

    merging xml files

    Nathan Coast, Oct 26, 2004, in forum: XML
    Replies:
    0
    Views:
    805
    Nathan Coast
    Oct 26, 2004
  3. Stefan Franke

    merging two XML files

    Stefan Franke, Jan 4, 2005, in forum: XML
    Replies:
    5
    Views:
    935
    Joris Gillis
    Jan 4, 2005
  4. Replies:
    5
    Views:
    775
    Jerry Coffin
    Sep 24, 2006
  5. n o s p a m p l e a s e

    Merging two DLL files

    n o s p a m p l e a s e, Jun 18, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,461
    CBFalconer
    Jun 19, 2007
  6. BlueBird
    Replies:
    4
    Views:
    368
    Gabriel Genellina
    Sep 17, 2007
  7. Tine Müller
    Replies:
    3
    Views:
    458
    Tine Müller
    Apr 30, 2008
  8. Albert-jan Roskam

    newbie - merging xls files using xldt and xlwt

    Albert-jan Roskam, Oct 15, 2008, in forum: Python
    Replies:
    5
    Views:
    933
    Jules Stevenson
    Oct 23, 2008
Loading...