免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: gtluck
打印 上一主题 下一主题

<请教>perl批量处理3000个文件 [复制链接]

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
1 [报告]
发表于 2017-03-04 12:05 |显示全部楼层
根据你提出的问题,我进行了一下整理,如有不正确的地方 请给予指正 谢谢。
1.>
如你所说,每个文件都是固定格式数据项。即
dog data
     name = bbbb...
     birthday = 20151201 14:23:23...
     gender=male...
      healthy = nok...
如上所示,可以看作一个完整的数据项。
其中有一个问题不是很理解,dog data 对于每个数据项来说是否为固定值?

2.>
筛选条件,查找数据项中具有指定值的 name 键。
并且
time-birthday > 10,这个值是如何得来的。请给出具体示例。
并且
healthy 不为 nok 的,该项的取值都有哪些?请给出示例。

最后,输出结果中。不论每个文件包含多少个数据项 只要有一个数据项满足以上条件 那么就显示该文件名? 还是有其他要求?

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
2 [报告]
发表于 2017-03-04 14:31 |显示全部楼层
time  -  birthday  >  10   即求两个的时间差,小于10个小时
你的要求是用文件的保存时间 即开头的 time 字段的年月日 如本例中的 20170101 与 数据项中的每个 birthday 键的完整日期时间进行减法操作 (20151201 14:23:23),
在其他条件满足的情况下,如果时间差 (小于 还是 大于) 10 小时的则满足输出条件 这样?

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
3 [报告]
发表于 2017-03-04 16:37 |显示全部楼层
本帖最后由 sunzhiguolu 于 2017-03-04 17:05 编辑

回复 5# gtluck
cat a.txt
------------------------
now:1488448457
dog data
       name = bbbb
       birthday = 20151201 14:23:23
       gender=male
        healthy = nok


dog data
       name = bbbb
       birthday = 20151201 14:23:23
       gender=male
        healthy = nok


dog data
       name = cccc
       birthday = 20151202 15:23:23
       gender=male
        healthy = nok

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Time::Local;
  5. use Time::Seconds;

  6. foreach my $path (glob ('urPath:\\*.txt')){
  7.     open (my $FHr, '<', $path);
  8.     my $now = $1 if (<$FHr> =~ /(\d+)/);
  9.     local $/ = '';
  10.     while (<$FHr>){
  11.         my @aItems = (split ("\n", $_))[1, 2, -1];
  12.         next if ($aItems[0] =~ /(\S+)\z/ and $1 ne 'bbbb');
  13.         next if ($aItems[-1] =~ /(\S+)\z/ and $1 eq 'nok');
  14.         my @aTimes = $aItems[1] =~ /(\d{4})(\d{2})(\d{2})\s+(\d+):(\d+):(\d+)/;
  15.         $aTimes[1] -= 1;
  16.         my $time = timegm (reverse (@aTimes));
  17.         next if (Time::Seconds->new ($now - $time)->hours <= 10);
  18.         print $path, "\n";
  19.         last;
  20.     }
  21.     close ($FHr);
  22. }
复制代码



论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
4 [报告]
发表于 2017-03-04 16:37 |显示全部楼层
本帖最后由 sunzhiguolu 于 2017-03-04 16:44 编辑

回复 5# gtluck
cat a.txt
------------------------
now:1488448457
dog data
       name = bbbb
       birthday = 20151201 14:23:23
       gender=male
        healthy = nok


dog data
       name = bbbb
       birthday = 20151201 14:23:23
       gender=male
        healthy = ok


dog data
       name = cccc
       birthday = 20151202 15:23:23
       gender=male
        healthy = nok

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Time::Local;
  5. use Time::Seconds;

  6. foreach my $path (glob ('urPath:\\*.txt')){
  7.     open (my $FHr, '<', $path);
  8.     my $now = $1 if (<$FHr> =~ /(\d+)/);
  9.     local $/ = '';
  10.     while (<$FHr>){
  11.         my @aItems = (split ("\n", $_))[1, 2, -1];
  12.         next if ($aItems[0] =~ /(\S+)\z/ and $1 ne 'bbbb');
  13.         next if ($aItems[-1] =~ /(\S+)\z/ and $1 eq 'nok');
  14.         my @aTimes = $aItems[1] =~ /(\d{4})(\d{2})(\d{2})\s+(\d+):(\d+):(\d+)/;
  15.         $aTimes[1] -= 1;
  16.         my $time = timegm (reverse (@aTimes));
  17.         next if (Time::Seconds->new ($now - $time)->hours <= 10);
  18.         print $path, "\n";
  19.         last;
  20.     }
  21.     close ($FHr);
  22. }
复制代码



论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
5 [报告]
发表于 2017-03-04 23:32 |显示全部楼层
本帖最后由 sunzhiguolu 于 2017-03-04 23:34 编辑

回复 10# gtluck
1.>
<$FHr> ===> now:1488448457
等价于
if (now:1488448457 =~ /(\d+)/)

2.>
"name = bbbb" =~ /(\S+)\z/
"name =bbbb" =~ /(\S+)\z/

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
6 [报告]
发表于 2017-03-05 19:21 |显示全部楼层
回复 15# gtluck
perl - Time::Local - timegm
NAMETime::Local - efficiently compute time from local and GMT time
SYNOPSIS
  •     $time = timelocal( $sec, $min, $hour, $mday, $mon, $year );
  •     $time = timegm( $sec, $min, $hour, $mday, $mon, $year );
DESCRIPTIONThis module provides functions that are the inverse of built-in perlfunctions localtime() and gmtime(). They accept a date as asix-element array, and return the corresponding time(2) value inseconds since the system epoch (Midnight, January 1, 1970 GMT on Unix,for example). This value can be positive or negative, though POSIXonly requires support for positive values, so dates before thesystem's epoch may not work on all operating systems.
It is worth drawing particular attention to the expected ranges forthe values provided. The value for the day of the month is the actualday (ie 1..31), while the month is the number of months since January(0..11). This is consistent with the values returned fromlocaltime() and gmtime().


论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
7 [报告]
发表于 2017-03-05 19:21 |显示全部楼层
回复 15# gtluck
perl - Time::Local - timegm
NAMETime::Local - efficiently compute time from local and GMT time
SYNOPSIS
  •     $time = timelocal( $sec, $min, $hour, $mday, $mon, $year );
  •     $time = timegm( $sec, $min, $hour, $mday, $mon, $year );
DESCRIPTIONThis module provides functions that are the inverse of built-in perlfunctions localtime() and gmtime(). They accept a date as asix-element array, and return the corresponding time(2) value inseconds since the system epoch (Midnight, January 1, 1970 GMT on Unix,for example). This value can be positive or negative, though POSIXonly requires support for positive values, so dates before thesystem's epoch may not work on all operating systems.
It is worth drawing particular attention to the expected ranges forthe values provided. The value for the day of the month is the actualday (ie 1..31), while the month is the number of months since January(0..11). This is consistent with the values returned fromlocaltime() and gmtime().


论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
8 [报告]
发表于 2017-03-06 11:12 |显示全部楼层

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Time::Local;
  5. use Time::Seconds;

  6. my $R = qr /\d\d/;
  7. my %hDogsList = map {$_ => 1} qw (aaa bbbb ccccc);

  8. foreach my $path (glob ('urPath:\\*.txt')){
  9.         open (my $FHr, '<', $path);
  10.         my $saveTime = $1 if (<$FHr> =~ /\Anow:(\d+)/);
  11.         local $/ = '';
  12.         my %hDog = ();
  13.         while (<$FHr>){
  14.                 foreach (split (/\n/)){
  15.                         y/[ \t]//d;
  16.                         my @aPairs = split (/=/);
  17.                         if (@aPairs % 2){
  18.                                 $hDog{'DD'} = $_;
  19.                                 next;
  20.                         }
  21.                         $hDog{$aPairs[0]} = $aPairs[-1];
  22.                 }
  23.                 my @aTimes = ($6, $5, $4, $3, $2 - 1, $1) if ($hDog{'birthday'} =~ /($R$R)($R)($R)($R):($R):($R)/);
  24.                 my $time = timegm (@aTimes);
  25.                 my $hours = Time::Seconds-> new ($saveTime - $time)->hours;
  26.                 next if (!$hDogsList{$hDog{'name'}} or $hDog{'healthy'} eq 'nok' or $hours <= 10);
  27.                 print $path, "\n";
  28.                 last;
  29.         }
  30.         close ($FHr);
  31. }
复制代码

论坛徽章:
307
程序设计版块每周发帖之星
日期:2016-04-08 00:41:33操作系统版块每日发帖之星
日期:2015-09-02 06:20:00每日论坛发贴之星
日期:2015-09-02 06:20:00程序设计版块每日发帖之星
日期:2015-09-04 06:20:00每日论坛发贴之星
日期:2015-09-04 06:20:00每周论坛发贴之星
日期:2015-09-06 22:22:00程序设计版块每日发帖之星
日期:2015-09-09 06:20:00程序设计版块每日发帖之星
日期:2015-09-19 06:20:00程序设计版块每日发帖之星
日期:2015-09-20 06:20:00每日论坛发贴之星
日期:2015-09-20 06:20:00程序设计版块每日发帖之星
日期:2015-09-22 06:20:00程序设计版块每日发帖之星
日期:2015-09-24 06:20:00
9 [报告]
发表于 2017-03-06 11:24 |显示全部楼层
本帖最后由 sunzhiguolu 于 2017-03-06 11:25 编辑

回复 18# jason680
大神 对于这个问题,还有其他的解决方法吗?还请您指点。。。

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP