How to substitute (regex) single newline (0A) character on Win32

Discussion in 'Perl Misc' started by msciwoj, Jul 17, 2009.

  1. msciwoj

    msciwoj Guest

    Does anyone have an idea how to do output single \n (0A) character
    instead of \r\n (0D0A) in win32?
    Let's say I do replacement:
    s/newlinemarker/\n/mg
    and want to substitute single character 0A
    Windows implementation seems to always be smarter and uses 0D0A
    automatically... How to prevent it?
    I tried:
    \n
    \r
    \x0a
    \cJ
    ....nothing worked so far...
     
    msciwoj, Jul 17, 2009
    #1
    1. Advertising

  2. msciwoj

    Steve C Guest

    msciwoj wrote:
    > Does anyone have an idea how to do output single \n (0A) character
    > instead of \r\n (0D0A) in win32?
    > Let's say I do replacement:
    > s/newlinemarker/\n/mg
    > and want to substitute single character 0A
    > Windows implementation seems to always be smarter and uses 0D0A
    > automatically... How to prevent it?
    > I tried:
    > \n
    > \r
    > \x0a
    > \cJ
    > ...nothing worked so far...


    binmode should work.
     
    Steve C, Jul 17, 2009
    #2
    1. Advertising

  3. msciwoj

    msciwoj Guest

    It is! Thanks!
     
    msciwoj, Jul 17, 2009
    #3
  4. msciwoj

    Guest

    On Fri, 17 Jul 2009 04:40:01 -0700 (PDT), msciwoj <> wrote:

    >Does anyone have an idea how to do output single \n (0A) character
    >instead of \r\n (0D0A) in win32?
    >Let's say I do replacement:
    >s/newlinemarker/\n/mg
    >and want to substitute single character 0A
    >Windows implementation seems to always be smarter and uses 0D0A
    >automatically... How to prevent it?
    >I tried:
    >\n
    >\r
    >\x0a
    >\cJ
    >...nothing worked so far...


    Well, you would like to write it out to a file in binmode, but
    that doesen't mean you have to set binmode on a read file to manipulate
    raw data internally.

    -sln
    -----------------------------------
    use strict;
    use warnings;

    while (<DATA>)
    {
    s/\x{0d}\x{0a}|\x{0d}/\x{0a}/;
    # to fixup stuff, say its mangled or something
    # (the cart or the horse, pick your poison)
    # s/\x{0a}\x{0d}/\x{0d}\x{0a}/;
    print;
    print ord($_), ' ' for ( split // );
    print "\n\n";

    # open a file for output (above 'while'), set to binmode,
    # then write it out here with a ord/split.
    }

    __DATA__
    line 1
    line 2
    line 3
    line 4
    ---------------------
    output:

    line 1
    108 105 110 101 32 49 10

    line 2
    108 105 110 101 32 50 10

    line 3
    108 105 110 101 32 51 10

    line 4
    108 105 110 101 32 52 10
     
    , Jul 17, 2009
    #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. Daniel Mark
    Replies:
    6
    Views:
    70,795
    Tim Chase
    Sep 28, 2006
  2. Peter Loftus
    Replies:
    5
    Views:
    114
    Tim Pease
    Apr 18, 2008
  3. Robert Lehman
    Replies:
    3
    Views:
    125
    Robert Lehman
    Mar 17, 2009
  4. Julia deSilva

    Result from a regex substitute

    Julia deSilva, Jul 29, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    90
    Julia deSilva
    Jul 29, 2003
  5. Mike Pearson

    Single-liner for one-line substitute?

    Mike Pearson, Jun 28, 2006, in forum: Perl Misc
    Replies:
    13
    Views:
    196
    Xicheng Jia
    Jun 30, 2006
Loading...

Share This Page