my perl script --- Trave Target directory and modify files with regex

來源:互聯網
上載者:User

標籤:

指令碼用於遍曆目錄下檔案,並用Regex匹配替換目標string.

#!/usr/bin/perl -wuse strict;# Trave Target directory and modify files under it.# Modified files are matched using regex.## V1.1use Getopt::Long;my $usage = q{Usage: fix_fetch_found [options] [dir]     Output is write back to files and marked with /*by script begin||end*/.     options:        dir             Dir to fix error that fetch and %found is null are out of order        -?,--help       Display this help message};# process the command linemy $help = 0;my $dirpath = $ARGV[0];GetOptions(    ‘help|?‘ => \$help,    ) or die $usage;if ($help) {    print $usage;    exit;}# main actionsmy $string = get_localtime($dirpath);my $log = $string.".log";my $modified_log = $string."_modified.log";Traversal($dirpath,$log);open(Log_File,"$log") or die("Could not open ‘$log‘ for reading $!");while(my $name = <Log_File>){chomp($name);my $flag = fix_file($name);if ($flag){write_file_a($modified_log,$name."\n");}}close(Log_File);exit;# fix file with filenamesub fix_file{my ($filename) = @_;my $flag = 0;my $filedata = read_file($filename);my $modified_data = modified_content($filedata);#print "check file $filename...\n";my $result = check_fix($modified_data,$filename);if ($result){ write_file_w($filename, $modified_data);print "fix file $filename!\n";$flag = 1;}return $flag;}# check whether file is modifiedsub check_fix{my ($file_1,$filename) = @_;my $result = 1;my $file_2 = read_file($filename);if ($file_1 eq $file_2){$result = 0;#print "$filename is not modified!\n";}return $result;}# modify file content using regexsub modified_content{my($content) = @_;#match string with /* FETCH VARS GUESSED */$content =~ s/IF\s*\n*\s*.*%ISOPEN\s*AND\s*\(.*%FOUND\s*IS\s*NULL\s*OR.*%FOUND\n*.*\)\s*THEN\n*\s*\/\* FETCH VARS GUESSED \*\/\s*FETCH\s*([\w_]+)\s*INTO([^;]*);/\/*by script begin*\/fetch $1 into $2;\n             if $1%isopen and $1%found then\/*by script end*\//gi;#match string without /* FETCH VARS GUESSED */$content =~ s/IF\s*\n*\s*.*%ISOPEN\s*AND\s*\(.*%FOUND\s*IS\s*NULL\s*OR.*%FOUND\n*.*\)\s*THEN\n*\s*FETCH\s*([\w_]+)\s*INTO([^;]*);/\/*by script begin*\/fetch $1 into $2;\n             if $1%isopen and $1%found then\/*by script end*\//gi;return $content;}# Trave dir and write all file path into a logsub Traversal{my ($path,$file) = @_;my $subpath;my $handle;my $record;if(-d $path){if(opendir($handle,$path)){while($subpath = readdir($handle)){if(!($subpath =~ m/^\.$/) and !($subpath =~ m/^(\.\.)$/)){my $p = $path."/$subpath";if (-d $p){Traversal($p,$file);}else{open($record,">>$file")||die("Could not open ‘$file‘ for writing $!");print $record($p."\n");close($record);} }}}closedir($handle);}return;}# get local time and append a string for file namesub get_localtime{my ($path) [email protected]_;(my $sec,my $min,my $hour,my $day,my $mon,my $year,my $wday,my $yday,my $isdst)=localtime(time());$year+=1900;$mon+=1;my $time = join("_",$year,$mon,$day,$hour,$min,$sec);my $str = $path."_".$time;return $str;}# read filesub read_file {my ($filename) = @_;  open my $in, ‘<‘, $filename or die "Could not open ‘$filename‘ for reading $!";local $/ = undef;# local $/ = undef can read all content into a variablemy $all = <$in>;close $in;  return $all;}# write file wsub write_file_w {my ($filename, $content) = @_;  open my $out, ‘>‘, $filename or die "Could not open ‘$filename‘ for writing $!";print $out $content;close $out;  return;}# write file asub write_file_a{my ($filename,$line) = @_;open (my $out,">>$filename") or die "Could not open ‘$filename‘ for writing $!";print $out $line;close $out;return;}

 

my perl script --- Trave Target directory and modify files with regex

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.