免费注册 查看新帖 |

Chinaunix

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

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

论坛徽章:
0
11 [报告]
发表于 2017-03-04 18:04 |只看该作者
回复 8# gtluck

传参的方式我找到了,谢谢。export and ENV

论坛徽章:
2
综合交流区版块每日发帖之星
日期:2016-07-06 06:20:00综合交流区版块每日发帖之星
日期:2016-08-16 06:20:00
12 [报告]
发表于 2017-03-04 19:27 |只看该作者
强烈支持楼主ing……

论坛徽章:
28
15-16赛季CBA联赛之八一
日期:2016-02-22 19:10:4215-16赛季CBA联赛之深圳
日期:2016-12-01 10:34:0415-16赛季CBA联赛之新疆
日期:2016-12-07 10:24:2915-16赛季CBA联赛之同曦
日期:2016-12-15 12:06:43CU十四周年纪念徽章
日期:2016-12-18 13:03:4415-16赛季CBA联赛之吉林
日期:2017-01-03 15:52:2515-16赛季CBA联赛之辽宁
日期:2017-01-04 14:58:2415-16赛季CBA联赛之辽宁
日期:2017-01-15 09:42:512016科比退役纪念章
日期:2017-02-06 17:21:50黑曼巴
日期:2017-02-10 15:46:1215-16赛季CBA联赛之上海
日期:2017-03-18 10:14:5415-16赛季CBA联赛之青岛
日期:2017-03-18 22:00:44
13 [报告]
发表于 2017-03-04 22:27 |只看该作者

  1. awk  -F' *= *' 'function t(s){match(s,/(....)(..)(..) (..):(..):(..)/,a);r="";for(i in a)if(+i&&length(i)==1)r=r?r OFS a[i]:a[i];return mktime(r)}NR<2{sub(/.*: */,"");st=$1}/name/{f=p=0;if($2==key)f=1}f&&/birthday/{time_diffs=(st-t($2))/3600;if(time_diffs>10)p=1}p&&/healthy/&&$2!="nok"{print FILENAME;nextfile}' key="aaaaaa" *.txt
复制代码

论坛徽章:
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
14 [报告]
发表于 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/

论坛徽章:
0
15 [报告]
发表于 2017-03-05 17:16 |只看该作者
回复 14# sunzhiguolu

many thanks!!

关于时间处理还有,最后一个疑问,烦请您帮忙解答:
  $aTimes[1] -= 1;
这里为撒要减1啊。

论坛徽章:
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
16 [报告]
发表于 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
17 [报告]
发表于 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().


论坛徽章:
145
技术图书徽章
日期:2013-10-01 15:32:13戌狗
日期:2013-10-25 13:31:35金牛座
日期:2013-11-04 16:22:07子鼠
日期:2013-11-18 18:48:57白羊座
日期:2013-11-29 10:09:11狮子座
日期:2013-12-12 09:57:42白羊座
日期:2013-12-24 16:24:46辰龙
日期:2014-01-08 15:26:12技术图书徽章
日期:2014-01-17 13:24:40巳蛇
日期:2014-02-18 14:32:59未羊
日期:2014-02-20 14:12:13白羊座
日期:2014-02-26 12:06:59
18 [报告]
发表于 2017-03-06 00:12 |只看该作者
回复 7# sunzhiguolu

这是什么编程, 关键词都不管的...

time:20170101
dog data
     name = aaaaaa...
     birthday = 20000101 15:00:00...
     gender=male...
     healthy = nok...
     woner = John
     tel = ...

现在的需求是读取指定name的dog data,如果文件中包含此name,而且time-birthday>10,healthy不为nok的

# 文档格式改变,或多或少立马出问题
my @aItems = (split ("\n", $_))[1, 2, -1];   

#要指定 ??
next if ($aItems[0] =~ /(\S+)\z/ and $1 ne 'bbbb');  

# 不在最后一行时 ??
next if ($aItems[-1] =~ /(\S+)\z/ and $1 eq '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
19 [报告]
发表于 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
20 [报告]
发表于 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