作者giacch (小a)
看板Perl
标题Re: [问题] 目录下的档案中比对字串
时间Sun Apr 11 02:44:54 2010
※ 引述《jammy8 (jay)》之铭言:
: 请问各位
: 假设在/tmp目录底下有/tmp/a .. z 的子目录
: 我想要在a...z子目录中寻找档案里有ERROR字样的档案
: 并且将找到的档案分别复制到/tmp/20100410的目录底下(随着日期而产生档名)
: 接着会放到crontab去做每30分钟的check
: 请问有好的方法可以推荐给我吗?
: 感恩阿
copy 过的档案不再检查, 也不会再 copy 一次, 直到换日期(换目录名称)
#!/usr/bin/perl
use File::Copy;
$dir='/tmp';
($mday, $mon, $year)=(localtime(time))[3 .. 5];
$dist=$dir.'/'.sprintf("%s%02d%02d", $year+1900, $mon+1, $mday);
foreach( grep { -d } glob($dir.'/[a-z]') ) {
/^$dist$/ and next or print "Search $_ ...\n";
foreach $file ( grep { -f } glob($_.'/*') ) {
($new=$file)=~s/.*?([^\/]+)$/$dist\/$1/;
print "File $new exists ...\n" and next if(-f $new);
open(READ, $file) or die "$file: $!\n";
$chk=join('', <READ>)=~/ERROR/;
close(READ);
(-d $dist) or mkdir($dist, 0755) and print "mkdir $dist\n" if($chk);
copy($file, $new) and print "copy $file to $new\n" if($chk);
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.192.170.66