[Perl] Text/code Bulk Substitution Tool _ Application Tips

Source: Internet
Author: User
Tags mkdir perl script
The Perl script batchreplace.pl can be used to bulk replace the text/code in the file. You can find files of the specified type in the specified directory and recursively check subdirectories, and copy the directory structure of the input file when you output the file.

Annex The Win32 application BatchReplace.exe is an executable program that is compiled from Perl scripts and can be executed without having to install the Perl runtime environment.


Using in the command line

batchreplace.exe[-I input file path (or directory containing files)] [-O Output file location (file or directory)] [-c], the extension of the bulk input file to "." Start, between multiple extensions with the "|" Separate] [-m matching mode] [-I (Ignores letter case of matching content)] [-G (Global lookup to match)] [---------------------------------------

There is no order limit for the above parameters. Automatically created when the file path that is involved with the-o parameter does not exist. When the output file already exists, the original contents of the file will be overwritten. (For security reasons, do not output to the original location of the input file, so as not to cause an unrecoverable loss.) )

For example:
Batchreplace.exe-i D:\gaoshu1-o d:\do\123\456-e http://www.blueidea.com/


Setting parameters through configuration file Batchreplace.set

The configuration file can contain the following setup items (format example, note case):


input=e:\fna\ Specifies the input path, which is equivalent to the default value of command line parameter-I.
-i=e:\fna\ ditto.
output=e:\dnawalks\ Specifies the output path, which is equivalent to the default value of the command-line parameter-O.
-o=e:\dnawalks\ ditto.
Match=<iframe[^>]*>[\s\s]*?<\/iframe> match mode, which is equivalent to the default value of the command line parameter-M.
-m=<iframe[^>]*>[\s\s]*?<\/iframe> ditto.
Insensitive ignores the letter case of the matching content, which is equivalent to the command line argument-I.
-I-ditto.
Global finds what to match, which is equivalent to the command line argument-G.
-G-ditto.
Replacement=-r=A string of except=http://www.blueidea.com/exceptions, such as a string found in the matching result, does not match, and is equivalent to the default value of the command line parameter-E.
-e=http://www.blueidea.com/ditto.
Checktype=.htm|. HTML when the input parameter is set to a directory, the text file that is contained in the directory that has these extensions (recursively checks subdirectories) is processed. Corresponds to the default value of the command line parameter-C.
-C Ibid.

Place one item per line. Do not have extra spaces or quotes between the contents.
All setup items are optional except for the necessary input and output. The command line arguments override the corresponding default values. If match or Checktype is not included in the configuration file, the built-in defaults are enabled (the values given in the example above). Replacement defaults to NULL characters, replaces matching content with null characters, which means that the matching content is purged.

To preserve the history configuration in Batchreplace.set, you can place any non-white-space characters before the arguments to remove their effects.
For example:
2007/04/06 insensitive
(The insensitive here will be retained as a historical configuration and no longer take effect.) )

2007/04/06 replacement=(The replacement=
If the same item appears in multiple lines of the configuration file, the last occurrence of the setting will be the same, for example:
Checktype=.htm|. Html
checktype=.jsp
-c=.asp|. Php
The value of the setting Checktype is. asp|. PHP, you can also use this feature to preserve the historical configuration for easy debugging.


This script is written in haste (it's time to squeeze out dinner) and I'll improve it later. (so reprint please indicate the source, and pay attention to the update.) )


[Disclaimer] The author does not assume any accidental loss caused by the use of this tool by the user.
Perl source
 code as follows:

#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 ($)} $c/gi) {die "$!"}
}
Else
{
Unless ($cont =~s/($match)/${$c =cexcept ($)} $c/i) {die "$!"}
}
}
Else
{
if ($global)
{
Unless ($cont =~s/($match)/${$c =cexcept ($)} $c/g) {die "$!"}
}
Else
{
Unless ($cont =~s/($match)/${$c =cexcept ($)} $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 (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. = ' \ n '. $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}
}


Packaged exe-file downloads
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.