Click here to get back home

Removing trailing newlines -

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Removing trailing newlines - John 04-23-2008
Posted by John on April 23, 2008, 7:31 am
Please log in for more thread options
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.

Posted by John on April 23, 2008, 7:56 am
Please log in for more thread options

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

Posted by Frank Seitz on April 23, 2008, 8:00 am
Please log in for more thread options
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.

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel

Posted by Ben Bullock on April 23, 2008, 8:20 am
Please log in for more thread options
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.

Posted by Ben Bullock on April 23, 2008, 8:19 am
Please log in for more thread options
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).


Similar ThreadsPosted
Removing trailing /, /index.php, and going up a directory March 23, 2006, 8:49 pm
How can I search and replace a string while preserving (not removing) trailing spaces? May 12, 2007, 11:43 pm
Newlines and deprecations February 21, 2007, 11:08 am
Substitute
for newlines
December 26, 2007, 4:37 pm
Loading a hash from a string containing newlines May 11, 2005, 5:52 pm
Deleting newlines in a text file January 11, 2005, 6:33 pm
XML::LibXML, newlines in nodes, and entities... July 6, 2005, 4:56 pm
Looking for Regexp that strips newlines inside of a tag August 26, 2005, 3:20 pm
Trouble with printing newlines to a file May 12, 2007, 5:54 am
Correct newlines for Perl programs in subversion? June 26, 2007, 10:21 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap