Re: [Comm] Re: [JT] Поиск и замена
Дорогов Николай
=?iso-8859-1?q?support_=CE=C1_planetashop=2Eru?=
Ср Апр 20 17:56:43 MSD 2005
Проблему поиска и замены текста в файлах с именами по маске
на половину решил следующим скриптом,
который нашел на сайте http://www.vanta.ru/script/info.php?id=498&clas=2
Не хватает прохода по вложенным папкам. А так просто сказочный скриптик.
#!/usr/bin/perl
# ====================================================================
# Copyright (c) 1998-2000 Marcus Kazmierczak, marcus на mkaz.com
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. The name of the author may not be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ====================================================================
#
#
# APP: mksr
# ORIGIN: August 1999
#
# VERSION NUMBER
$ver = "ver 0.0.5";
# Last Modified: November 2000
#
# Auth: Marcus Kazmierczak
# marcus на mkaz.com
# http://blazonry.com/perl/
#
# DESC: Search and Replace
#
print "\nmksr Search and Replace $ver\n";
print "Marcus Kazmierczak, marcus\@mkaz.com\n\n";
# TODO LIST
#
# - ADD Recursion Option
## REQUIRED CPAN MODULES ##
# File::Recurse Module bundled with File-Tools 2.0
# download from CPAN site
(http://www.perl.com/CPAN//modules/by-module/File/)
# use File::Recurse;
####
my $find = $ARGV[0];
my $replace = $ARGV[1];
my $glob = $ARGV[2];
@filelist = <*$glob>;
if ( (!$find) || (!$replace) || (!$glob) ) {
print "Search and replace recursively through the current directory\n";
print "replacing <find> with <replace> in each file specified.\n";
print "To use wildcards leave off the * Ex: '.txt' \n\n";
print " mksr <find> <replace> <file>\n";
exit(0);
}
# process each file in file list
foreach $filename (@filelist) {
print " P: $filename\n";
# retrieve complete file
open (IN, "$filename") || die("Error Reading File: $filename $!");
{
undef $/;
$infile = <IN>;
}
close (IN) || die("Error Closing File: $filename $!");
$infile =~ s/$find/$replace/g;
# write complete file
open (PROD, ">$filename") || die("Error Writing to File: $filename
$!");
print PROD $infile;
close (PROD) || die("Error Closing File: $filename $!");
}
print "\nFinished.\n";
exit(0);
----- Original Message -----
From: "Andrey Rahmatullin" <wrar на altlinux.ru>
To: <community на altlinux.ru>
Sent: Wednesday, April 20, 2005 5:28 PM
Subject: Re: [Comm] Re: [JT] Поиск и замена
> _______________________________________________
> Community mailing list
> Community на altlinux.ru
> https://lists.altlinux.ru/mailman/listinfo/community
Подробная информация о списке рассылки community