use strict; use warnings; my %eols=( unix => "\x0A", linux => "\x0A", dos => "\x0D\x0A", win => "\x0D\x0A", mac => "\x0D", apple => "\x0D", ); sub usage () { print STDERR "Usage $0 ".join('|', sort keys %eols)."\n"; exit(8); } sub determin_eol($$) { my ($last,$ch)=@_; my $write=''; # Win Ending if($last eq "\x0D" && $ch eq "\x0A") { $write=$eol; $last=''; } # Mac Ending elsif($last eq "\x0D") { $write=$eol; $last=$ch; } # UNX Ending elsif($last eq "\x0A") { $write=$eol; $last=$ch; } # no ending found else { $write=$last; $last=$ch; } return ($write,$last); } binmode(STDIN); binmode(STDOUT); usage() unless (@ARGV && $eols{$ARGV[0]}); my $eol=$eols{$ARGV[0]}; my $ch; my $last=''; while (sysread(STDIN, $ch, 1) > 0) { my $write=''; ($write,$last)=determin_eol($last,$ch); syswrite(STDOUT, $write); } # write last char. syswrite(STDOUT, join('',determin_eol($last,'')));