分析したいファイルが10万個あります。具体的には、任意のサイズのファイルのサンプルから印刷可能な文字の割合を計算したいと思います。これらのファイルの一部はメインフレーム、Windows、Unixなどからのものであるため、バイナリ文字と制御文字が含まれている可能性があります。
Linuxの「file」コマンドを使用して開始しましたが、目的に十分な詳細が提供されませんでした。次のコードは、私がやろうとしていることを伝えていますが、常に機能するとは限りません。
#!/usr/bin/perl -n
use strict;
use warnings;
my $cnt_n_print = 0;
my $cnt_print = 0;
my $cnt_total = 0;
my $prc_print = 0;
#Count the number of non-printable characters
while ($_ =~ m/[^[:print:]]/g) {$cnt_n_print++};
#Count the number of printable characters
while ($_ =~ m/[[:print:]]/g) {$cnt_print++};
$cnt_total = $cnt_n_print + $cnt_print;
$prc_print = $cnt_print/$cnt_total;
#Print the # total number of bytes read followed by the % printable
print "$cnt_total|$prc_print\n"
これは機能するテスト呼び出しです。
echo "test_string of characters" | /home/user/scripts/prl/s16_count_chars.pl
これは私がそれを呼ぶつもりであり、1つのファイルに対して機能します:
find /fct/inbound/trans/ -name "TRNST.20121115231358.xf2" -type f -print0 | xargs -0 head -c 2000 | /home/user/scripts/prl/s16_count_chars.pl
これは正しく機能しません:
find /fct/inbound/trans/ -type f -print0 | xargs -0 head -c 2000 | /home/user/scripts/prl/s16_count_chars.pl
これもしません:
find /fct/inbound/trans/ -type f -print0 | xargs -0 head -c 2000 | perl -0 /home/user/scripts/prl/s16_count_chars.pl
検索によって返された各行に対してスクリプトを1回実行する代わりに、すべての結果に対して1回実行します。
前もって感謝します。
これまでの調査:
パイプとXARGSおよびセパレーター
http://help.lockergnome.com/linux/help-understand-pipe-xargs--ftopict549399.html
http://en.wikipedia.org/wiki/Xargs#The_separator_problem
明確化:
1。)必要な出力:ディレクトリに932ファイルがある場合、出力は、ファイル名、ファイルから読み取られた合計バイト数、および印刷可能な文字である%の932行のリストになります。
2.)ファイルの多くはバイナリです。スクリプトは、埋め込まれたバイナリeol
またはeof
シーケンスを処理する必要があります。
3.)ファイルの多くは大きいので、最初/最後のxxバイトだけを読み取りたいと思います。私は最初の256バイトまたは最後の128バイトをそれぞれhead -c 256
またはうとしていtail -c 128
ソリューションは、パイプラインで機能するか、perlスクリプト内のバイトを制限することができます。