in reply to Process file regardless of the platform it was created on

s/\n?\r/\r/g? (This preserves empty lines, too)

Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.

Replies are listed 'Best First'.
Re^2: Process file regardless of the platform it was created on
by andreas1234567 (Vicar) on May 20, 2008 at 08:25 UTC
    Yes, I guess a s/\r\n/\n/g followed by chomp would do it:
    $ cat unixordos.pl use strict; use warnings; my $fh = undef; open($fh, "<", "foo.txt") or die "failed to open 'foo.txt':$!"; while (my $line = <$fh>) { $line =~ s/\r\n/\n/g; chomp $line; ($line =~ m/^\d+$/) ? print qq{number ok:"$line"} : print qq{number error:"$line"}; print "\n"; } close($fh) or die "failed to close 'foo.txt':$!"; __END__ $ echo -e "123\n456\n789" > foo.txt $ perl unixordos.pl number ok:"123" number ok:"456" number ok:"789" $ unix2dos foo.txt unix2dos: converting file foo.txt to DOS format ... $ perl unixordos.pl number ok:"123" number ok:"456" number ok:"789" $
    Will this work generally?
    --
    No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
      I guess a s/\r\n/\n/g followed by chomp would do it

      That substitution is exactly what the crlf PerlIO layer is doing (on input). The layer is by default in effect when you're on Windows (which is why you don't have the problem with Windows' files on Windows :)

      In other words, you could also do

      open($fh, "<:crlf", "foo.txt") or die "failed to open 'foo.txt':$!";

      It should work AFAIK.

      It too converts single CR to LF (mac format?)

      Rule One: Do not act incautiously when confronting a little bald wrinkly smiling man.