Perl脚本:递归替换目录下所有源文件中指定字符串
来源:互联网 发布:市政工程bim软件 编辑:程序博客网 时间:2024/06/08 06:06
如需要将目录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 failed\n";
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$item\n/\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 failed\n";
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 directory\n"; # exclude test directory
if ($cur_file =~ /^\.svn$/) { # ignore svn directories
print $File::Find::name, " is svn directory, skip\n";
$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);
- Perl脚本:递归替换目录下所有源文件中指定字符串
- 使用shell命令sed将指定目录下的所有文件中指定的字符串替换成指定的字符串
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
- 替换文件夹中所有文件中指定字符串
- Java 替换文件夹下所有文件中指定的内容
- Perl脚本递归替换所有指定目录指定文件类型中的指定模式
- 字符串中指定字符串的替换
- 递归重命名目录中指定文件
- linux 替换文件中指定字符串 方法
- 替换掉mysql数据库指定字段的所有数据的中指定字符串
- PHP替换字符串中指定字符串 替换指定的次数
- perl,替换当前目录下所有文件中的某个串
- 使用sh脚本如何替换指定目录下所有文件中的指定字符串
- 用递归方法列出目录,在命令行中指定目录。
- 替换一个规则字符串中指定的几个字符串
- php 正则替换 字符串中指定的字符串
- java 多线程查找文件夹下所有文件中指定的字符串
- 用批处理实现文本文件中指定字符串的替换 zz
- 解决TortoiseCVS在Win7下重叠图标不显示的问题
- Web based Desktop--open source rich-client technology
- CSS颜色代码
- C++实现的委托机制(1)
- ASP.net页面中请求远程Web站点
- Perl脚本:递归替换目录下所有源文件中指定字符串
- WP7开发之SQLite数据库
- 设计模式实例教程
- 关于ACE_TP_Reactor
- xxxx回归测试问题汇总(第七期).xls
- iphone实现上拉刷新的方法(Andy)
- 谈谈cPanel的网站备份功能
- The project cannot be built until build path errors are resolved
- WP7永久保存数据方法