Zesa Rex Zesa Rex - 14 days ago 5
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

  2. delete

  3. replace

  4. replace

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

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
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";

mob mob

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