Removing trailing newlines -

Discussion in 'Perl Misc' started by John, Apr 23, 2008.

  1. John

    John Guest

    I want to read a file line-by line and write it line by line to another file.
    Regarless of environment I want newline start with DOS's \x0d\x0a

    Here's the code snippet:

    $ii=open(MYHAN,"<file01.htm");
    open(MYHAN2,">>receive.htm");
    binmode(MYHAN);
    binmode(MYHAN2);
    while ($line=<MYHAN>)
    {
    chomp($line);
    $line=~s/\x0d//g; # probably unnecessary
    $line=~s/\x0a//g;
    print MYHAN2 $rivi."testing\x0d\x0a";
    }
    close MYHAN;
    closeMYHAN2;


    The problem is that I get in "receive.txt" I get ending
    "balhblahtesting[CR][CR][LF]" where [CR] mean carriage return and [LF] line feed.

    Why is this happening? I've chomped and ~s'd the $line. I've also binmoded both
    file handles for good measure.
     
    John, Apr 23, 2008
    #1
    1. Advertising

  2. John

    John Guest

    John <> wrote:

    >I want to read a file line-by line and write it line by line to another file.
    >Regarless of environment I want newline start with DOS's \x0d\x0a
    >
    >Here's the code snippet:
    >

    $ii=open(MYHAN,"<file01.htm");
    open(MYHAN2,">>receive.htm");
    binmode(MYHAN);
    binmode(MYHAN2);
    while ($line=<MYHAN>)
    {
    chomp($line);
    $line=~s/\x0d//g; # probably unnecessary
    $line=~s/\x0a//g;
    print MYHAN2 $line."testing\x0d\x0a";
    }
    close MYHAN;
    close MYHAN2;

    oops there was a typo - I had cut the code from another source but problem
    remains teh same.

    >
    >
    >The problem is that I get in "receive.txt" I get ending
    >"balhblahtesting[CR][CR][LF]" where [CR] mean carriage return and [LF] line feed.
    >
    >Why is this happening? I've chomped and ~s'd the $line. I've also binmoded both
    >file handles for good measure.
     
    John, Apr 23, 2008
    #2
    1. Advertising

  3. John

    Ben Bullock Guest

    On Wed, 23 Apr 2008 14:31:18 +0300, John wrote:

    > Why is this happening? I've chomped and ~s'd the $line. I've also
    > binmoded both file handles for good measure.


    I rewrote your code as follows and didn't get the error you mention on
    either Ubuntu Linux or Windows or Cygwin:

    #!/usr/bin/perl
    use warnings;
    use strict;
    open(MYHAN,"<", "testcrs.pl") or die $!;
    open(MYHAN2,">", "receive.txt") or die $!;
    binmode(MYHAN);
    binmode(MYHAN2);
    while (my $line=<MYHAN>)
    {
    chomp($line);
    $line=~s/\x0d//g; # probably unnecessary
    $line=~s/\x0a//g;
    print MYHAN2 $line." testing R\x0dS\x0aT";
    }
    close MYHAN;
    close MYHAN2;

    Note that the "binmode" is essential here - without that what you
    describe is the expected behaviour on Windows. The most likely cause of
    the problem is that "open (MYHAN2" ... actually failed and you were
    looking at an old version of the file before you'd used the "binmode"
    statement, or perhaps you didn't scroll down far enough (since originally
    it was appending to receive.txt rather than overwriting it).
     
    Ben Bullock, Apr 23, 2008
    #3
  4. John

    Ben Bullock Guest

    On Wed, 23 Apr 2008 14:00:44 +0200, Frank Seitz wrote:

    > John wrote:
    >>
    >> The problem is that I get in "receive.txt" I get ending
    >> "balhblahtesting[CR][CR][LF]" where [CR] mean carriage return and [LF]
    >> line feed.
    >>
    >> Why is this happening? I've chomped and ~s'd the $line.

    >
    > chomp() removes the value of $/ - i.e. LF on Unix, not CRLF.


    That can't be the solution, because he also used global substitutions to
    remove any line feeds or carriage returns in the string $line.
     
    Ben Bullock, Apr 23, 2008
    #4
  5. John

    John Guest

    Ben Bullock <> wrote:

    >On Wed, 23 Apr 2008 14:31:18 +0300, John wrote:
    >
    >> Why is this happening? I've chomped and ~s'd the $line. I've also
    >> binmoded both file handles for good measure.

    >
    >I rewrote your code as follows and didn't get the error you mention on
    >either Ubuntu Linux or Windows or Cygwin:
    >
    >#!/usr/bin/perl
    >use warnings;
    >use strict;
    >open(MYHAN,"<", "testcrs.pl") or die $!;
    >open(MYHAN2,">", "receive.txt") or die $!;
    >binmode(MYHAN);
    >binmode(MYHAN2);
    >while (my $line=<MYHAN>)
    > {
    > chomp($line);
    > $line=~s/\x0d//g; # probably unnecessary
    > $line=~s/\x0a//g;
    > print MYHAN2 $line." testing R\x0dS\x0aT";
    > }
    >close MYHAN;
    >close MYHAN2;
    >
    >Note that the "binmode" is essential here - without that what you
    >describe is the expected behaviour on Windows. The most likely cause of
    >the problem is that "open (MYHAN2" ... actually failed and you were
    >looking at an old version of the file before you'd used the "binmode"
    >statement, or perhaps you didn't scroll down far enough (since originally
    >it was appending to receive.txt rather than overwriting it).




    I copied you example and still get the problem. The receive.txt looks like below
    #!/usr/bin/perl testing R
    S
    Tuse warnings; testing R
    S
    Tuse strict; testing R
    S
    T testing R
    S
    Tprint "Content-Type: text/html; charset=iso-8859-1\n\n"; testing R
    S
    .....

    I added some html lines:

    #!/usr/bin/perl
    use warnings;
    use strict;

    print "Content-Type: text/html; charset=iso-8859-1\n\n";
    print '<html>';
    print '<head>';
    print '<title>Hello</title>';
    print '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">';
    print '</head>';


    open(MYHAN,"<", "testcrs.pl") or die $!;
    open(MYHAN2,">", "receive.txt") or die $!;
    binmode(MYHAN);
    binmode(MYHAN2);
    while (my $line=<MYHAN>)
    {
    chomp($line);
    $line=~s/\x0d//g; # probably unnecessary
    $line=~s/\x0a//g;
    print MYHAN2 $line." testing R\x0dS\x0aT";
    }
    close MYHAN;
    close MYHAN2;

    print '</html>';
     
    John, Apr 23, 2008
    #5
  6. John

    Ben Bullock Guest

    On Wed, 23 Apr 2008 15:35:47 +0300, John wrote:

    > I copied you example and still get the problem.


    It's probably an error elsewhere, not in Perl.
     
    Ben Bullock, Apr 23, 2008
    #6
  7. John

    John Guest

    Ben Bullock <> wrote:

    >On Wed, 23 Apr 2008 15:35:47 +0300, John wrote:
    >
    >> I copied you example and still get the problem.

    >
    >It's probably an error elsewhere, not in Perl.


    You are right! My bad. Wasted hours on this. The problem was that when I
    downloaded the file the ftp client program was set to "Auto" and it added the
    extra CR's. When I set it to "Binary" the files came down OK.

    Thanks for the help!
     
    John, Apr 23, 2008
    #7
  8. John

    J. Gleixner Guest

    John wrote:

    > I copied you example and still get the problem. The receive.txt looks like below

    [...]
    > I added some html lines:


    Yeah, that's usually the first step in debugging any program.

    1. Always add HTML. That'll fix 99% of your coding errors.

    :)
     
    J. Gleixner, Apr 24, 2008
    #8
    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. Ben
    Replies:
    5
    Views:
    2,123
    jongalloway
    Jan 16, 2005
  2. Russell Warren

    logging module and trailing newlines

    Russell Warren, Oct 2, 2007, in forum: Python
    Replies:
    2
    Views:
    273
    Russell Warren
    Oct 3, 2007
  3. Dotan Cohen
    Replies:
    2
    Views:
    513
    Dotan Cohen
    Oct 10, 2011
  4. Replies:
    2
    Views:
    258
  5. Replies:
    6
    Views:
    136
    Thomas 'PointedEars' Lahn
    Jan 27, 2008
Loading...

Share This Page