4

我有 100,000 个文件要分析。具体来说,我想从任意大小的文件样本中计算可打印字符的百分比。其中一些文件来自大型机、Windows、Unix 等,因此很可能包含二进制和控制字符。

我从使用 Linux 的“文件”命令开始,但它没有为我的目的提供足够的细节。以下代码传达了我想要做的事情,但并不总是有效。

    #!/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

这就是我打算调用它的方式,并且适用于一个文件:

    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

它不是为查找返回的每一行执行一次脚本,而是为所有结果执行一次。

提前致谢。


迄今为止的研究:

管道和 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.) 许多文件是二进制文件。脚本需要处理嵌入的二进制eoleof序列。
3.)许多文件很大,所以我只想读取第一个/最后一个 xx 字节。我一直试图分别使用head -c 256tail -c 128读取前 256 个字节或后 128 个字节。解决方案可以在管道中工作,也可以在 perl 脚本中限制字节。

4

3 回答 3

4

-n选项将您的整个代码包装在一个while(defined($_=<ARGV>) { ... }块中。这意味着您my $cnt_print和其他变量声明对于每一行输入都会重复,本质上是重置所有变量值。

解决方法是使用全局变量(our如果要继续使用,请使用 声明它们use strict),而不是将它们初始化为0,因为它们将为每一行输入重新初始化。你可以说类似

our $cnt_print //= 0;

如果你不想$cnt_print和它的朋友在第一行输入中未定义。

看到这个最近的问题有一个类似的问题。

于 2012-11-20T22:33:21.863 回答
1

你可以find一次给你一个参数。

find /fct/inbound/trans/ -type f -exec perl script.pl {} \;

但我会继续一次传递多个文件,xargs要么find通过-exec +.

find /fct/inbound/trans/ -type f -exec perl script.pl {} +

以下代码片段支持两者。

您可以一次继续阅读一行:

#!/usr/bin/perl

use strict;
use warnings;

my $cnt_total   = 0;
my $cnt_n_print = 0;

while (<>) {
    $cnt_total += length;
    ++$cnt_n_print while /[^[:print:]]/g;
} continue {
    if (eof) {
        my $cnt_print = $cnt_total - $cnt_n_print;
        my $prc_print = $cnt_print/$cnt_total;

        print "$ARGV: $cnt_total|$prc_print\n";

        $cnt_total   = 0;
        $cnt_n_print = 0;
    }
}

或者您可以一次读取整个文件:

#!/usr/bin/perl

use strict;
use warnings;

local $/;
while (<>) {
    my $cnt_n_print = 0;
    ++$cnt_n_print while /[^[:print:]]/g;

    my $cnt_total = length;
    my $cnt_print = $cnt_total - $cnt_n_print;
    my $prc_print = $cnt_print/$cnt_total;

    print "$ARGV: $cnt_total|$prc_print\n";
}
于 2012-11-20T22:51:58.203 回答
0

这是我根据提供的反馈的 工作解决方案。

我将不胜感激有关形式或更有效方法的任何进一步反馈:

    #!/usr/bin/perl

    use strict;
    use warnings;

    # This program receives a file path and name.
    # The program attempts to read the first 2000 bytes.
    # The output is a list of files, the number of bytes
    # actually read and the percent of tbe bytes that are
    # ASCII "printable" aka [\x20-\x7E].

    my ($data, $n_bytes, $file_name, $cnt_n_print, $cnt_print, $prc_print);

    # loop through each file
    foreach(@ARGV) {
       $file_name = shift or die "Pass the file name on the command line.\n";

       # open the file read only with "<" in "<$file_name"
       open(FILE, "<$file_name") or die "Can't open $file_name: $!";

       # open each file in binary mode to handle non-printable characters
       binmode FILE;

       # try to read 2000 bytes from FILE, save the results in $data and the
       # actual number of bytes read in $n_bytes
       $n_bytes = read FILE, $data, 2000;

       $cnt_n_print = 0;
       $cnt_print = 0;

       # count the number of non-printable characters
       ++$cnt_n_print while ($data =~ m/[^[:print:]]/g);

       $cnt_print = $n_bytes - $cnt_n_print;
       $prc_print = $cnt_print/$n_bytes;

       print "$file_name|$n_bytes|$prc_print\n";
       close(FILE);
    }

以下是如何调用上述脚本的示例:

    find /some/path/to/files/ -type f -exec perl this_script.pl {} +

以下是我发现有用的参考文献列表:

POSIX括号表达式
以binmode打开文件读取
功能
打开文件只读

于 2012-11-28T19:15:03.487 回答