Mazdajai
asked on
unable to remove invisible characters with perl
I have a file with special characters that I would like to replace.
I found the regex to detect it but I am not able to replace it, am I doing something wrong here?
Code:
File:
I found the regex to detect it but I am not able to replace it, am I doing something wrong here?
Code:
use feature qw(say);
# Detect non-printable characters with POSIX expression
my $inv_rex = qr/[^[:print:]]/;
my $inv_rex_s = s/[^[:print:]]//;
my $matched = 0;
while (my $line = <>) {
chomp $line;
@fields = split / /, $line;
# Check each field
foreach my $field (@fields) {
next unless $field =~ $inv_rex;
$field =~ $inv_rex_s;
$matched++;
}
}
say "\n $matched fields with non-printable characters detected";
File:
D:\scripts\>cat -vet d:\temp\foo.txt
M-oM-;M-?A 10.10.29.40 mdmaaa.example.comM-BM- ^M$
A 10.10.29.41 mytssd.example.comM-BM- ^M$
A 10.10.29.42 netfwcc.example.comM-BM- ^M$
A 10.10.29.43 nj6bfvv.example.comM-BM- ^M$
A 10.10.29.44 njovvv.example.comM-BM- ^M$
A 10.10.29.45 njtfff4.example.comM-BM- ^M$
A 10.10.29.46 oiddd.example.comM-BM- ^M$
A 10.10.29.47 ovdlll.example.comM-BM-
foo.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER