如何使用Perl从目录中的所有文件中提取模式?

时间:2010-10-08 09:04:10

标签: regex perl

我正在运行一个命令,该命令会在特定日期的每个小时返回96 .txt个文件。 所以最后它在一个目录中给了我一天24 * 96个文件。 我的目标是提取四个月的数据,这将导致目录中的30 * 24 * 96 * 4个文件。

获取数据后,我需要从每个文件中提取某些“模式”并将其显示为输出。

1)下面的脚本仅适用于脚本中硬编码日期的一天 2)我需要在一个月的所有日子里工作,我需要从6月到10月运行它 3)由于数据量巨大,我的磁盘空间不足,所以我不想创建这么多文件,而只想动态grep并只获取一个输出文件。

我怎样才能有效地做到这一点?

我的shell脚本看起来像这样

for R1 in {0..9}; do
  for S1 in {0..95}; do

      echo $R1 $S1

      curl  -H "Accept-Encoding: gzip" "http://someservice.com/getValue?Count=96&data=$S1&fields=hitType,QueryString,pathInfo" | zcat > 20101008-mydata-$R1-$S1.txt
  done
done
  • 这将返回我需要的文件。
  • 之后,我从每个文件grep“test / link / link2”* |中提取一个URL模式grep category> 1.输出

2 个答案:

答案 0 :(得分:0)

您可以使用此awk命令获取网址

awk -vRS="</a>" '/href/&&/test.*link2/&&/category/{gsub(/.*<a.*href=\"|\".*/,"");print}' file

答案 1 :(得分:0)

以下是如何循环超过4个月的日期

#!/usr/bin/perl
use strict;
use warnings;
use Date::Simple ':all';

for (my $date = ymd(2010,4,1), my $end = ymd(2010,8,1);$date < $end; $date++) {
    my $YYYYMMDD = $date->format("%Y%m%d");
    process_one_day($YYYYMMDD); # Add more formats if needed as parameters
}

sub process_one_day {
    my $YYYYMMDD = shift;
    # ...
    # ... Insert your code to process that date
    # ... Either call system() command on the sample code in your question
    # ... Or better yet write a native Perl equivalent
    # ...
    # ... For native processing, use WWW::Mechanize to extract the data from the URL
    # ... and Perl's native grep() to grep for it
}