Zesa Rex Zesa Rex - 1 month ago 7
Perl Question

How do I remove \r without removing \n?

so I will try to make it as simple as possible. I am formatting files right now. These are the tasks that the script should complete:


  1. delete
    \r\n

  2. delete
    &

  3. replace
    #A
    with
    \n#A

  4. replace
    #D
    with
    \n#D



My code is already working but it also adds
\r
in front on every
\n
added, and that's something I don't want because
\r
isn't recognized by the program I want to import my files to. Do you know a method to only delete
\r
without removing also the
\n
?

Here's my code. I guess only the last
$data =~ s!\r!!g;
is relevant. Why isn't this working like intented? There is still
\r\n
after every row...

opendir my $dir_a, $scriptdir or die "Kann Verzeichnis nicht oeffnen: $!";
my @stammdateien = grep { /root/ } readdir $dir_a;
closedir $dir_a;

foreach my $stammdatei (@stammdateien){
if ($stammdatei eq "root101" or $stammdatei eq "root112" or $stammdatei eq "root116"){
my $stammdateipfad = "$scriptdir\\$stammdatei";
my $data = lese_file($stammdateipfad);
$data =~ s!\r|\n!!g;
$data =~ s!&!!g;
$data =~ s!#A!\n#A!g;
$data =~ s!#D!\n#D!g;
$data =~ s!#M \| INSERT \|!#M \| UPDATE_INSERT \|!g;
$data =~ s!\r!!g;
schreibe_file($stammdateipfad, $data);
}
}

sub lese_file {
my ($stammdatei) = @_;
open my $in, "<", $stammdatei or die "Konnte die Datei '$stammdatei' nicht zum lesen oeffnen: $!";
local $/ = undef;
my $all = <$in>;
close $in;
return $all;
}


sub schreibe_file {
my ($stammdateipfadout, $data) = @_;
my $outputdatei = "$stammdateipfadout"."_CONVERTED";
open my $out, ">", "$outputdatei" or die "Konnte die Datei $outputdatei nicht zum schreiben oeffnen: $!";
print $out $data;
close $out;
print "====> Vorgang abgeschlossen!\n\n";
return;
}

mob mob
Answer

Are you on a Windows platform? The default IO layer translates \n to \r\n on Windows. To disable that, try one of:

open my $out, ">", "$outputdatei" or ...;
binmode $out;

open my $out, ">:raw", "$outputdatei" or
Comments