Last active
May 2, 2024 07:14
-
-
Save azumakuniyuki/a44b4d2391e4d7ec620cf906959a9c30 to your computer and use it in GitHub Desktop.
Find multibyte characters from the specified files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# macOSの/usr/bin/grepでマルチバイト文字の検索が上手くいかないのでPerlでどうにかする | |
use strict; | |
use warnings; | |
use IO::File; | |
die sprintf("Usage: %s file1 [file2 [file3 ...]]\n", $0) unless @ARGV; | |
for my $fn ( @ARGV ) { | |
# ファイルが無い・読めない・空の場合は無視する | |
next unless -f $fn; | |
next unless -r $fn; | |
next unless -s $fn; | |
my $io = IO::File->new($fn, 'r') || next; | |
my $la = sprintf("- %s", $fn); | |
my $ln = 0; | |
my $cv = []; | |
while( my $e = $io->getline ) { | |
# Find any multibyte characters | |
$ln++; | |
chomp $e; next unless $e; | |
next if $e =~ /^[\x00-\x7e]+$/; | |
push @$cv, sprintf(" - %04d: %s", $ln, $e); | |
} | |
$io->close; | |
next unless @$cv; | |
printf("%s\n", $la); | |
printf("%s\n", $_ ) for @$cv; | |
} |
Author
azumakuniyuki
commented
May 2, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment