Re: [問題] 目錄下的檔案中比對字串

看板Perl作者 (小a)時間14年前 (2010/04/11 02:44), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
※ 引述《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
文章代碼(AID): #1BmCSfKd (Perl)
文章代碼(AID): #1BmCSfKd (Perl)