加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl脚本:递归替换目录下所有源文件中指定字符串

发布时间:2020-12-16 00:32:40 所属栏目:大数据 来源:网络整理
导读:如需要将目录dir1下的所有源文件中的单词static和inline删除,则建立文件 string.txt: static inline 再运行: $?perl ./replace_string dir1 ? replace_string的内容如下: #!/bin/perl # This script is to replace strings specified in a file with blan

如需要将目录dir1下的所有源文件中的单词static和inline删除,则建立文件

string.txt:

static

inline

再运行:

$?perl ./replace_string dir1

?

replace_string的内容如下:

#!/bin/perl
# This script is to replace strings specified in a file with blank. The target directory is treated recursively.
#
# Usage:
# perl ./this_script directory_name
#


use strict;
use warnings;
use File::Find;

my $affected_file_num = 0;
my $replacement_occurence = 0;

sub get_filter_str_arry {

??? my $repstr_filename = "string.txt";

??? # establish replacement string array
??? open(my $repstr_file,"<",$repstr_filename) or die "Open replacement string file failedn";
??? my @str_arry;
??? while (<$repstr_file>) {
??????? chomp;
??????? for my $str (split) {
??????????? push(@str_arry,$str);
??????? }
??? }

?# dump the replacement strings
??? print "***** replacement string array ********n";
??? foreach my $item (@str_arry) {
??????? print $item,"n";
??? }
??? print "***************************************n";

??? close $repstr_file;
??? return @str_arry;
}

my @str_arry = get_filter_str_arry();

sub process_source_file {

??? my $data_filename = $_[0];
??? my $full_path = $_[1];
???
??? # start processing data file
??? open(my $data_file,$data_filename) or die "Open data file failed $full_path: $!n";

??? # read the whole file
??? $/ = undef;
??? my $body = <$data_file>;
??? my $old_body = $body;

??? foreach my $cur_item (@str_arry) {
??????? my $item = $cur_item;

??????? $body =~ s/b$itemn/n/g;
??????? $body =~ s/b$item[ |t]+//g;
??? }

??? if ($body ne $old_body) {??# further operations only when needed
??????? open(my $new_file,">","tmp_new_file") or die "new failedn";
??????? print $new_file $body;

??????? close $new_file;
??????? close $data_file;

??????? rename($data_filename,"$data_filename.orig");
??????? rename("tmp_new_file",$data_filename);
??? } else {
??????? close $data_file;
??? }

}

sub process_file {
??? # $File::Find::dir is the current directory name
??? # $_ is the current filename within that directory
??? # $File::Find::name is the complete pathname to the file
???
??? my $cur_file = $_;?
??? if (-d $cur_file) {?# directory
??????? print $File::Find::name," is a directoryn";?# exclude test directory

??????? if ($cur_file =~ /^.svn$/) {?# ignore svn directories
??????????? print $File::Find::name," is svn directory,skipn";
??????????? $File::Find::prune = 1;
??????? }

?} else {?# file
??????? if ($cur_file =~ /.(c|h|def|inc)$/i) {???? # filter file type

??????????? print $cur_file,"n";
??????????? print "$_ = ",$_,",File::Find::name = ",$File::Find::name,"n";?
??????????? process_source_file($cur_file,$File::Find::name);

??????? }
??? }
}

my @DIRLIST = @ARGV;? find(&;process_file,@DIRLIST);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读