[Perl]文字/代码批量替换工具
Perl脚本batchReplace.pl可以用来批量替换文件中的文字/代码。可在指定目录中查找指定类型的文件,并递归检查子目录;在输出文件时复制输入文件的目录结构。
[附件]Win32应用程序batchReplace.exe是由Perl脚本编译产生的可执行程序,不需安装Perl运行环境即可执行。
在命令行中使用
batchReplace.exe[ -i 输入文件路径(或包含文件的目录)][ -o 输出文件位置(文件或目录)][ -c 批量输入文件的扩展名,以“.”开始,多个扩展名之间以“|”隔开][ -m 匹配模式][ -I(忽略匹配内容的字母大小写)][ -G(全局查找要匹配的内容)][ -e 例外的字符串,是对匹配模式的补充,如果在匹配结果中发现有这样的字符串,做不匹配处理][ -r 替换的内容]
上述参数没有顺序限制。当 -o 参数所涉及的文件路径不存在时,会自动创建。当输出文件已经存在时,文件原有的内容将被覆盖。(安全起见,请不要输出到输入文件的原始位置,以免造成不可恢复的损失。)
例如:
batchReplace.exe -i d:\gaoshu1 -o d:\do\123\456 -e http://www.blueidea.com/
通过配置文件 batchReplace.set 设置参数
配置文件中可包含以下设置项目(格式范例,注意大小写):
Input=E:\fna\ 指定输入路径,相当于命令行参数 -i 的默认值。
-i=E:\fna\ 同上。
Output=E:\dnaWalks\ 指定输出路径,相当于命令行参数 -o 的默认值。
-o=E:\dnaWalks\ 同上。
Match=<iframe[^>]*>[\s\S]*?<\/iframe> 匹配模式,相当于命令行参数 -m 的默认值。
-m=<iframe[^>]*>[\s\S]*?<\/iframe> 同上。
Insensitive 忽略匹配内容的字母大小写,相当于命令行参数 -I。
-I 同上。
Global 全局查找要匹配的内容,相当于命令行参数 -G。
-G 同上。
Replacement=<h1>bound0</h1> 替换的内容,相当于命令行参数 -r 的默认值。
-r=<h1>bound0</h1> 同上。
Except=http://www.blueidea.com/ 例外的字符串,如在匹配结果中发现有这样的字符串,做不匹配处理,相当于命令行参数 -e 的默认值。
-e=http://www.blueidea.com/ 同上。
CheckType=.htm|.html 当输入参数设为目录时,处理目录中包含的具有这些扩展名的文本文件(递归检查子目录)。相当于命令行参数 -c 的默认值。
-c 同上。
每行放置一个项目。除内容之间不要有多余的空格或引号。
除必要的输入输出外,所有的设置项目都是可选的。命令行参数会覆盖相应的默认值。如果在配置文件中不包含Match或CheckType,会启用内置的默认值(同上面示例中给出的值)。Replacement默认为空字符,将匹配内容替换为空字符,也就是将匹配内容清除。
欲在batchReplace.set中保留历史配置时,可在参数前放置任意非空白字符以取消其作用。
例如:
2007/04/06 Insensitive
(此处的 Insensitive 将作为历史配置保留,不再生效。)
2007/04/06 Replacement=<h1>bound0</h1>
(此处的 Replacement=<h1>bound0</h1> 将作为历史配置保留,不再生效。)
如果在配置文件的多行中出现同样的项目,以最后出现的设置为准,例如:
CheckType=.htm|.html
CheckType=.jsp
-c=.asp|.php
将设置CheckType的值为.asp|.php,也可以利用这个特性保留历史配置,方便调试。
这个脚本写得比较仓促(是挤出吃晚饭的时间写的),以后有时间我还会改进。(因此转载请注明出处,并注意更新。)
[免责声明]作者不承担用户因使用此工具而造成的任何意外损失。
perl源码
复制代码 代码如下:
#file:batchReplace.pl
#author:Bound0
#created:2007-04-06
#first published: http://bbs.blueidea.com/viewthread.php?tid=2734388
my $match;
my $replacement='';
my $insensitive=0;
my $global=0;
my $gi;
my $go;
my $Checktype=".htm|.html";
my $except;
if(open(setfile,"<batchReplace.set"))
{
while(<setfile>)
{
if(/^\s*-I/){$insensitive=1}
if(/^\s*-G/){$global=1}
if(/^\s*-m=(.+)/){$match=$1}
if(/^\s*-r=(.+)/){$replacement=$1}
if(/^\s*-e=(.+)/){$except=$1}
if(/^\s*-i=(.+)/){$gi=$1}
if(/^\s*-o=(.+)/){$go=$1}
if(/^\s*-c=(.+)/){$Checktype=$1}
if(/^\s*Insensitive/){$insensitive=1}
if(/^\s*Global/){$global=1}
if(/^\s*Match=(.+)/){$match=$1}
if(/^\s*Replacement=(.+)/){$replacement=$1}
if(/^\s*Except=(.+)/){$except=$1}
if(/^\s*Input=(.+)/){$gi=$1}
if(/^\s*Output=(.+)/){$go=$1}
if(/^\s*CheckType=(.+)/){$Checktype=$1}
}
}
my $para=' '.join(' ',@ARGV);
if($para=~/ -I */){$insensitive=1}
if($para=~/ -G */){$global=1}
my @ti=split(/ -i */,$para);
if($ti[1]){($gi)=split(/ -(o|i|c|e|m|r|I|G)/,$ti[1])}
unless($gi){print "No \"Input path\" parameter!";exit}
my @to=split(/ -o */,$para);
if($to[1]){($go)=split(/ -(o|i|c|e|m|r|I|G)/,$to[1])}
unless($go){print "No \"Output path\" parameter!";exit}
my @tc=split(/ -c */,$para);
if($tc[1]){($Checktype)=split(/ -(o|i|c|e|m|r|I|G)/,$tc[1])}
my @te=split(/ -e */,$para);
if($te[1]){($except)=split(/ -(o|i|c|e|m|r|I|G)/,$te[1])}
my @tr=split(/ -r */,$para);
if($tr[1]){($replacement)=split(/ -(o|i|c|e|m|r|I|G)/,$tr[1])}
unless($match){$match="<iframe[^>]*>[\\s\\S]*?<\\/iframe>";
$insensitive=1;
$global=1}
my @tm=split(/ -m */,$para);
if($tm[1]){($match)=split(/ -(o|i|c|e|m|r|I|G)/,$tm[1])}
unless($match){print "No \"Match Pattern\" parameter!";exit}
my $checktyp='(';
$Checktype=~s/\./\\\./g;
$Checktype=~s/\|/\)\|\(/g;
$checktyp.=$Checktype.')$';
my $excep;
if($except){
$excep=$except;
$excep=~s/\//\\\//g;
$excep=~s/\./\\\./g;
$excep=~s/\|/\\\|/g;
$excep=~s/\[/\\\[/g;
$excep=~s/\]/\\\]/g;
$excep=~s/\(/\\\(/g;
$excep=~s/\)/\\\)/g;
$excep=~s/\$/\\\$/g;
$excep=~s/\?/\\\?/g;
}
my $replacemen;
if($replacement){
$replacemen=$replacement;
$replacemen=~s/\//\\\//g;
$replacemen=~s/\./\\\./g;
$replacemen=~s/\|/\\\|/g;
$replacemen=~s/\[/\\\[/g;
$replacemen=~s/\]/\\\]/g;
$replacemen=~s/\(/\\\(/g;
$replacemen=~s/\)/\\\)/g;
$replacemen=~s/\$/\\\$/g;
$replacemen=~s/\?/\\\?/g;
}
sub cFile
{
my $fi;
($fi)=@_;
if(opendir(DIR, $fi))
{
my @dir=readdir(DIR);
closedir DIR;
if("\\" eq substr $fi,(length $fi)-1){$fi=substr($fi,0,(length $fi)-1)}
my @subdirs= grep { /^(?!\.)/ && -d "$fi\\$_" } @dir;
foreach my $subdir (@subdirs)
{
cFile("$fi\\$subdir")
}
@files = grep { /$checktyp/i && -T "$fi\\$_" } @dir;
foreach my $fil (@files)
{
my $bp='';
$bp=(substr $fi,(length $gi))."\\";
my $bi="$fi\\$fil";
my $bo=$go.$bp.$fil;
remove($bi,$bo)
}
}
}
unless("\\" eq substr $go,(length $go)-1){$go.="\\"}
if(-d $gi)
{
unless("\\" eq substr $gi,(length $gi)-1){$gi.="\\"}
cFile($gi);
}
else
{
my $bu=substr $gi,(rindex $gi,'\\');
my $bo=$go.$bu;
remove($gi,$bo)
}
print "\nProcess Finished!";
print "\n-i:$gi";
print "\n-o:$go";
print "\n-m:$match";
if($except){print "\n-e:$except"}
if($replacement){print "\n-r:$replacement"}
sub remove
{
my $bi;
my $bo;
($bi,$bo)=@_;
print "\nprocessing $bi ...\n";
unless(open(INPUT,"<$bi")){print "\n[Warn] Can not open the file <$bi>: $!";return}
my @conts = <INPUT>;
close INPUT;
my $cont=join '',@conts;
my $c;
if($insensitive)
{
if($global)
{
unless($cont=~s/($match)/${$c=Cexcept($1)}$c/gi){die "$!"}
}
else
{
unless($cont=~s/($match)/${$c=Cexcept($1)}$c/i){die "$!"}
}
}
else
{
if($global)
{
unless($cont=~s/($match)/${$c=Cexcept($1)}$c/g){die "$!"}
}
else
{
unless($cont=~s/($match)/${$c=Cexcept($1)}$c/){die "$!"}
}
}
unless(open(OUT, ">$bo"))
{
if($!==2)
{
my $dbo=substr $bo,0,(rindex $bo,'\\');
if(opendir(OUTDIR,$dbo)){closedir OUTDIR;print "\n[Warn] Can not open the output file <$bo>: $!";exit}
else
{
if($!==2)
{
unless(pmkpath($dbo)){print "\n[Warn] Can not creat the output directory <$dbo>: $!";exit}
unless(open(OUT,">>$bo")){print "\n[Warn] Can not open the output file <$bo>: $!";exit}
}
else{print "\n[Warn] Can not open the output directory <$dbo>: $!";exit}
}
}
else{print "\n[Warn] Can not open the output file <$bo>: $!";exit}
}
print OUT "$cont";
close OUT;
}
sub pmkpath
{
my @p=split(/\\/,shift);
my $pa=$p[0];
my $m=$#p+1;
my $t;
for($t=1; -e $pa;$t++){$pa.='\\'.$p[$t]}
unless(mkdir $pa){return 0}
for(;$t<$m;$t++)
{
$pa.='\\'.$p[$t];
unless(mkdir $pa){return 0}
}
return 1
}
sub Cexcept
{
unless($except){return $replacemen}
my $con;
($con)=@_;
if($con=~/$excep/){return $con}else{return $replacemen}
}
打包的exe文件下载
相关文章
- 要替换字符串中的内容我们只要利用php相关函数,如strstr,str_replace,正则表达式了,那么我们要替换目录所有文件的内容就需要先遍历目录再打开文件再利用上面讲的函数替...2016-11-25
jQuery实现滑动页面固定顶部显示(可根据显示位置消失与替换)
本文实例讲述了jQuery实现滑动页面固定顶部显示(可根据显示位置消失与替换)。分享给大家供大家参考,具体如下:这里介绍的jQuery拉动页面固定顶部显示,及自动消失效果,可能ie浏览器下有问题,不过火狐什么的都可以运行看效果...2015-10-30- 这两天在用Perl编写一些监控脚本,其实写代码也是一件挺有意思的事情,就是挺废时间的。而且,由于语法不太熟,基本想到一个东西都要先Google一下看怎么实现。...2020-06-29
- 下面小编就为大家带来一篇Perl与JS的对比分析(数组、哈希)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-29
- 这篇文章主要介绍了用golang实现替换某个文件中的字符串操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-25
- 一、简介 模式指在字符串中寻找的特定序列的字符,由反斜线包含:/def/即模式def。其用法如结合函数split将字符串用某模式分成多个单词:@array = split(/ /, $line);二、匹配...2020-06-29
- Perl时间函数localtime的使用介绍,这里简单的介绍下,更多请查看官方介绍...2020-06-29
- 这篇文章给大家介绍了Idea使用正则表达式批量替换字符串的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧...2021-07-21
- 这篇文章主要介绍了Perl中的特殊内置变量详细介绍,需要的朋友可以参考下...2020-06-29
- 这篇文章主要介绍了讲Perl中的本地时间与UNIX时间戳间相互转换的方法,主要用到了Perl中的Date::Parse模块,需要的朋友可以参考下...2020-06-29
- 一、列表 列表是包含在括号里的一序列的值,可以为任何数值,也可为空,如:(1, 5.3 , "hello" , 2),空列表:()。 注:只含有一个数值的列表(如:(43.2) )与该数值本身(即:43.2 )是不同...2020-06-29
- 这篇文章主要介绍了JS基于正则截取替换特定字符之间字符串操作方法,结合具体实例形式分析了JS基于正则实现针对特殊字符、数字等字符串类型的截取操作相关技巧,需要的朋友可以参考下...2017-02-08
- 这篇文章主要为大家详细介绍了Opencv实现绿幕视频背景替换功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
- 这篇文章主要介绍了Perl学习笔记之CPAN使用介绍,本文讲解了什么是CPAN、CPAN的目录作用介绍、CPAN安装Perl Module的两种方法等内容,需要的朋友可以参考下...2020-06-29
- 对所有输入文件,如果没有版权信息则加上版权信息,否则什么都不做,并对原文件以.bak结尾备份,需要的朋友可以参考下...2020-06-29
- 这篇文章主要介绍了Perl中使用MIME::Lite发送邮件实例,本文介绍了使用sendmail方式发送、发送HTML格式邮件、smtp方式发送邮件等内容,需要的朋友可以参考下...2020-06-29
- 这篇文章主要介绍了Perl AnyEvent中的watcher实例,关于AnyEvent请参阅的更多介绍请参阅文中的相关链接,需要的朋友可以参考下...2020-06-29
jQuery实现滑动页面固定顶部显示(可根据显示位置消失与替换)
本文实例讲述了jQuery实现滑动页面固定顶部显示(可根据显示位置消失与替换)。分享给大家供大家参考,具体如下:这里介绍的jQuery拉动页面固定顶部显示,及自动消失效果,可能ie浏览器下有问题,不过火狐什么的都可以运行看效果...2015-10-30- C#里面的String.Replace(string,string)方法替换的时候是替换所有的匹配项,我们需要只替换第一个匹配项,写一个方法来实现这个功能...2020-06-25
- perl获取日期与时间的例子,供大家学习参考...2020-06-29