当前位置: 移动技术网 > IT编程>网页制作>Perl > [Perl]文字/代码批量替换工具

[Perl]文字/代码批量替换工具

2017年12月08日  | 移动技术网IT编程  | 我要评论
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文件下载

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网