I have a string that is read from a text file, but in Ubuntu Linux, and I try to delete its newline character from the end.
I used all the ways. But for s/\n|\r/-/
(I look whether it finds any replaces any new line string) it replaces the string, but it still goes to the next line when I print it. Moreover, when I used chomp
or chop
, the string is completely deleted. I could not find any other solution. How can I fix this problem?
use strict;
use warnings;
use v5.12;
use utf8;
use encoding "utf-8";
open(MYINPUTFILE, "<:encoding(UTF-8)", "file.txt");
my @strings;
my @fileNames;
my @erroredFileNames;
my $delimiter;
my $extensions;
my $id;
my $surname;
my $name;
while (<MYINPUTFILE>)
{
my ($line) = $_;
my ($line2) = $_;
if ($line !~ /^(((\X|[^\W_ ])+)(.docx)(\n|\r))/g) {
#chop($line2);
$line2 =~ s/^\n+//;
print $line2 . " WRONG FORMAT!\n";
}
else {
#print "INSERTED:".$13."\n";
my($id) = $13;
my($name) = $2;
print $name . "\t" . $id . "\n";
unshift(@fileNames, $line2);
unshift(@strings, $line2 =~ /[^\W_]+/g);
}
}
close(MYINPUTFILE);
Best Answer
The correct way to remove Unicode linebreak graphemes, including CRLF pairs, is using the
\R
regex metacharacter, introduced in v5.10.The
use encoding
pragma is strongly deprecated. You should either use theuse open
pragma, or use an encoding in the mode argument on 3-argopen
, or usebinmode
.