【问题标题】:Executing zgrep recursively in Linux在 Linux 中递归执行 zgrep
【发布时间】:2009-09-01 22:54:59
【问题描述】:

当给定 1989 年 2 月 26 日所有 24 小时的巨大输入日志文件时,此 zgrep 命令正在输出包含单词黄色的行的特定字段。

zgrep 'yellow' /color_logs/1989/02/26/*/1989-02-26-00_* | cut -f3 -d'+' 

1) 我更喜欢使用 perl 脚本。使用 bash 脚本有什么好处吗?

此外,在编写此脚本时,我希望它在处理每一天的数据后创建一个文件(因此它会查看一天中的所有时间)

zgrep 'yellow' /color_logs/1989/02/*/*/1989-02-26-00_* | cut -f3 -d'+' 

2) 在处理一天的数据后,如何确定第一个星的值(在 Perl 中),以便我可以输出名称中带有 YYMMDD 的文件。我有兴趣从该问题正上方的代码行中获取第一颗星的值。

【问题讨论】:

    标签: linux perl grep


    【解决方案1】:

    Grep 写出该行来自的文件,但您的 cut 命令将其丢弃。你可以这样做:

    open(PROCESS, "zgrep 'yellow' /color_logs/1989/02/*/*/1989-02-26_* |");
    while(<PROCESS>) {
        if (m!/color_logs/(\d\d\d\d)/(\d\d)/(\d\d)/[^:]+:(.+)$!) {
            my ($year, $month, $day, $data) = ($1, $2, $3, $4);
            # Do the cut -f3 -d'+' on the line from the log
            my $data = (split('+', $data))[2];
            open(OUTFILE, ">>${year}${month}${day}.log");
            print OUTFILE $data, "\n";
            close(OUTFILE);
        }
    }
    

    因为您要为每一行打开和关闭文件,所以效率低下,您可以使用 IO::File 对象,并且仅在日期更改时打开,但您明白了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-27
      • 2021-10-01
      • 2015-11-14
      • 2023-01-20
      • 1970-01-01
      • 2013-03-13
      • 2015-06-25
      • 2014-03-14
      相关资源
      最近更新 更多