问题产生:跑随机仿真的时候,往往会有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!!!";
}
}