我一直在尝试编写Perl脚本来替换项目所有源文件中的某些文本。我需要类似的东西:

perl -p -i.bak -e "s/thisgoesout/thisgoesin/gi" *.{cs,aspx,ascx}

但是,递归解析所有目录的文件

我刚刚开始编写脚本:

use File::Find::Rule;
use strict;

my @files = (File::Find::Rule->file()->name('*.cs','*.aspx','*.ascx')->in('.'));

foreach my $f (@files){
    if ($f =~ s/thisgoesout/thisgoesin/gi) {
           # In-place file editing, or something like that
    }
}

但现在我被卡住了。有没有一种使用Perl编辑所有文件的简单方法?

请注意,我不需要保留每个修改过的文件的副本;我有他们所有的颠覆=)

更新:我在 Cygwin 上试过这个,

perl -p -i.bak -e "s/thisgoesout/thisgoesin/gi" {*,*/*,*/*/*}.{cs,aspx,ascx

但看起来我的参数列表爆炸到允许的最大大小。事实上,我在Cygwin上遇到了非常奇怪的错误...

有帮助吗?

解决方案

如果在使用@ARGV(又名菱形*ARGV)之前分配<>,则$^I / -i将对这些文件起作用,而不是在命令行中指定的文件。

use File::Find::Rule;
use strict;

@ARGV = (File::Find::Rule->file()->name('*.cs', '*.aspx', '*.ascx')->in('.'));
$^I = '.bak';  # or set `-i` in the #! line or on the command-line

while (<>) {
    s/thisgoesout/thisgoesin/gi;
    print;
}

这应该完全符合您的要求。

如果您的模式可以跨越多行,请在undef $/;之前添加<=>,以便Perl一次对整个文件进行操作,而不是逐行操作。

其他提示

您可能对 File :: Transaction :: Atomic 或文件::交易

F :: T :: A的概要与您尝试的内容非常相似:

  # In this example, we wish to replace 
  # the word 'foo' with the word 'bar' in several files, 
  # with no risk of ending up with the replacement done 
  # in some files but not in others.

  use File::Transaction::Atomic;

  my $ft = File::Transaction::Atomic->new;

  eval {
      foreach my $file (@list_of_file_names) {
          $ft->linewise_rewrite($file, sub {
               s#\bfoo\b#bar#g;
          });
      }
  };

  if ($@) {
      $ft->revert;
      die "update aborted: $@";
  }
  else {
      $ft->commit;
  }

结合File :: Find你已经写好了,你应该好好去。

您可以使用Tie :: File来可伸缩地访问大文件并在适当的位置更改它们。请参阅联机帮助页(man 3perl Tie :: File)。

更改

foreach my $f (@files){
    if ($f =~ s/thisgoesout/thisgoesin/gi) {
           #inplace file editing, or something like that
    }
}

foreach my $f (@files){
    open my $in, '<', $f;
    open my $out, '>', "$f.out";
    while (my $line = <$in>){
        chomp $line;
        $line =~ s/thisgoesout/thisgoesin/gi
        print $out "$line\n";
    }
}

这假设图案不跨越多行。如果模式可能跨越行,则需要在文件内容中插入。 (<!>“slurp <!>”是一个非常常见的Perl术语。)

chomp实际上并不是必需的,我刚刚被那些没有多次chomp的行所咬过(如果你放弃print $out "$line\n";,将print $out $line;改为open my $out, '>', "$f.out";)。

同样,您可以将open my $out, '>', undef;更改为<=>以打开临时文件,然后在替换完成后将该文件复制回原始文件。事实上,特别是如果你在整个文件中啜食,你可以简单地在内存中进行替换,然后在原始文件上进行写入。但是我犯了足够的错误,我总是写一个新文件,并验证内容。


注意,我最初在该代码中有一个if语句。这很可能是错的。这只会复制到与正则表达式匹配的行<!> quot; thisgoesout <!> quot; (当然)用<!> quot; thisgoesin <!>来代替它,同时默默地吞噬其余部分。

您可以使用find

find . -name '*.{cs,aspx,ascx}' | xargs perl -p -i.bak -e "s/thisgoesout/thisgoesin/gi"

这将以递归方式列出所有文件名,然后xargs将读取其stdin并运行命令行的其余部分,并在末尾附加文件名。关于-i的一个好处是,如果它构建的命令行太长而无法一次运行,它将多次运行命令行。

请注意,我不确定<=>是否完全理解选择文件的所有shell方法,因此如果上述方法不起作用,那么可以尝试:

find . | grep -E '(cs|aspx|ascx)$' | xargs ...

当使用这样的管道时,我喜欢建立命令行并在继续之前单独运行每个部分,以确保每个程序都获得它想要的输入。因此,您可以在没有<=>的情况下运行该部件进行检查。

我刚刚想到,尽管您没有这么说,但由于您正在寻找的文件后缀,您可能在Windows上。在这种情况下,可以使用Cygwin运行上述管道。您可以编写一个Perl脚本来执行相同的操作,但是您必须自己进行就地编辑,因为在这种情况下您无法利用<=>开关。 / p>

感谢这个问题和这个问题。回答,我明白了:

use File::Find::Rule;
use strict;

sub ReplaceText {
    my $regex = shift;
    my $replace = shift;

    @ARGV = (File::Find::Rule->file()->name('*.cs','*.aspx','*.ascx')->in('.'));
    $^I = '.bak';
    while (<>) {
        s/$regex/$replace->()/gie;
        print;
    }
}

ReplaceText qr/some(crazy)regexp/, sub { "some $1 text" };

现在我甚至可以遍历包含regexp = <!> gt; subs条目的哈希!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top