文件夹数量巨大下的关键字搜索

问题产生:跑随机仿真的时候,往往会有5w或者10w的文件夹生成,随机跑完后需要把关键error信息筛选出来,单纯的使用grep会报出数量太多无法识别的情况

问题解决:用perl脚本抓取想要的信息,好处是:1.不受文件夹里文件数量的限制,2.可以在大规模随机仿真后即可进行error搜索,不需要再单独去用时间搜索一遍,搜索一遍的时间可能在半个小时级别以上。

#! /usr/bin/perl

use 5.010;
use strict;
#use warnings;
use Getopt::Long;
use File::Find;

my ($sec,$min,$hour,$day,$mon,$year,$weekday,$yeardate,$savinglightday)   =  (localtime(time));
$sec   =   ($sec   <   10)?   "0$sec":$sec;
$min   =   ($min   <   10)?   "0$min":$min;
$hour   =   ($hour   <   10)?   "0$hour":$hour;
$day   =   ($day   <   10)?   "0$day":$day;
$mon   =   ($mon   <   9)?   "0".($mon+1):($mon+1);
$year   +=   1900;
my $date = "${year}${mon}${day}";

GetOptions (
            "full"  => \&func_full,
            "delete"  => \&rm_dir,
) or &usage;

sub usage {
        say "f or d must be specified with option";
    }

sub func_full {
        say "full function is starting!!!";
        &pre_main_cal;
        &search_error;
    }

sub pre_main_cal {

if (-e "good_file.txt") {
say "good_file.txt is exists";
unlink "good_file.txt";
say "good_file.txt is delete";
}

if (-e "error.txt") {
say "error.txt is exists";
unlink "error.txt";
say "error.txt is delete";
}


my @dir = '.';
my @all_files;

open my $good_file_fh,'>','good_file.txt';

find(\&PrintName,@dir);

close $good_file_fh;

sub PrintName{
    my $path;
        $path = $File::Find::name;
        if ($path =~ /enc_.*\/enc_.*\.log$/){
            say $good_file_fh "$path";
            push @all_files ,$path;
        }
    }
    }

sub search_error {
    open my $file,'<',"good_file.txt";
    open my $new_file,'>',"error.txt";
    while(<$file>){
        chomp;
        my $file_name = $_;
        open my $new_file_text,"$_";
        while(<$new_file_text>){
               chmod;
               my $line = $_;
                if($line =~ /UVM_ERROR :/){
                   my $file_name_connect = "$file_name---$line"; 
                     printf $new_file "$file_name_connect";
                }
        }
    close $new_file_text;
    }
    close $new_file;
close $file;

sub rm_dir {
   say "simulation dir are begin removing!!!";
   system("rm -rf enc*"); 
   say "simulation dir are removed!!!";
}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值