[Perl]文字/代碼批量替換工具

來源:互聯網
上載者:User

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檔案下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.