当前位置:编程学习 > 网站相关 >>

Perl脚本:递归替换目录下所有源文件中指定字符串

 

如需要将目录dir1下的所有源文件中的单词static和inline删除,则建立文件

 

string.txt:

 

static

 

inline

 

再运行:

 

$ perl ./replace_string dir1

 

 

 

replace_string的内容如下:

 

#!/bin/perl

# This script is to replace strings specified in a file with blank. The target directory is treated recursively.

#

# Usage:

# perl ./this_script directory_name

#

 

 

use strict;

use warnings;

use File::Find;

 

my $affected_file_num = 0;

my $replacement_occurence = 0;

 

sub get_filter_str_arry {

 

    my $repstr_filename = "string.txt";

 

    # establish replacement string array

    open(my $repstr_file, "<", $repstr_filename) or die "Open replacement string file failed\n";

    my @str_arry;

    while (<$repstr_file>) {

        chomp;

        for my $str (split) {

            push(@str_arry, $str);

        }

    }

 

 # dump the replacement strings

    print "***** replacement string array ********\n";

    foreach my $item (@str_arry) {

        print $item, "\n";

    }

    print "***************************************\n";

 

    close $repstr_file;

    return @str_arry;

}

 

my @str_arry = get_filter_str_arry();

 

sub process_source_file {

 

    my $data_filename = $_[0];

    my $full_path = $_[1];

   

    # start processing data file

    open(my $data_file, "<", $data_filename) or die "Open data file failed $full_path: $!\n";

 

    # read the whole file

    $/ = undef;

    my $body = <$data_file>;

    my $old_body = $body;

 

    foreach my $cur_item (@str_arry) {

        my $item = $cur_item;

 

        $body =~ s/\b$item\n/\n/g;

        $body =~ s/\b$item[ |\t]+//g;

    }

 

    if ($body ne $old_body) {  # further operations only when needed

        open(my $new_file, ">", "tmp_new_file") or die "new failed\n";

        print $new_file $body;

 

        close $new_file;

        close $data_file;

 

        rename($data_filename, "$data_filename.orig");

        rename("tmp_new_file", $data_filename);

    } else {

        close $data_file;

    }

 

}

 

sub process_file {

    # $File::Find::dir is the current directory name

    # $_ is the current filename within that directory

    # $File::Find::name is the complete pathname to the file

   

    my $cur_file = $_;

    if (-d $cur_file) { # directory

        print $File::Find::name," is a directory\n"; # exclude test directory

 

        if ($cur_file =~ /^\.svn$/) { # ignore svn directories

            print $File::Find::name, " is svn directory, skip\n";

            $File::Find::prune = 1;

        }

 

 } else { # file

        if ($cur_file =~ /\.(c|h|def|inc)$/i) {     # filter file type

 

            print $cur_file, "\n";

            print "\$_ = ", $_, ", File::Find::name = ", $File::Find::name, "\n";

            process_source_file($cur_file, $File::Find::name);

 

        }

    }

}

 

 

my @DIRLIST = @ARGV;

find(\&process_file, @DIRLIST);

 

  摘自:金卓军的博客

补充:Web开发 , 其他 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,