【问题标题】:Perl replacing Strings from files in directoryPerl 从目录中的文件替换字符串
【发布时间】:2014-06-05 04:05:49
【问题描述】:

我正在尝试制作一个替换目录中所有文件中的字符串的程序。问题是我需要让它只有在句子的开头或结尾,或两者兼而有之时才可以更改。这是我目前所拥有的:

use strict;
use warnings;
use File::Find;     #This lets us use the find() function, similar to 'find' in Unix 
                # -- which is especially helpful for searching recursively.

print "Which directory do you want to use?\n"; #Ask what directory to use
my $dir = readline STDIN;
chomp $dir; #Used for eliminating the newline character '\n'

print "What String would you like to search for?\n"; #Ask for what String to search for.
my $search = readline STDIN;
chomp $search;

print "What String would you like to replace it with?\n"; #Ask for what to replace it with.
my $replace = readline STDIN;
chomp $replace;

print "Would you like to replace $search if it is at the beginning of the sentence? (y/n) \n";
my $beg = readline STDIN;
chomp $beg;

print "Would you like to replace $search if it is at the end of the sentence? (y/n) \n";
my $end = readline STDIN;
chomp $end;


find(\&txtrep, $dir); #This function lets us loop through each file in the directory

sub txtrep  {
    if ( -f and /.txt$/) { # Proceeds only if it is a regular .txt file
        my $file = $_;  # Set the name of the file, using special Perl variable
        open (FILE , $file);
        my @lines = <FILE>; #Puts the file into an array and separates sentences
        my @lines2 = split(".", @lines);
        close FILE;

        if ($beg eq "y") {
            foreach my $slot (@lines2) {
                $slot =~ s/^$search/$replace/gi;
            }
        }

        if ($end eq "y") {
            foreach my $slot (@lines2) {
                $slot =~ s/$search$/$replace/gi;
            }
        }

        open (FILE, ">$file");
        print FILE @lines2;
        close FILE;
    }
}

在我运行这个之后,它只是删除了文件中的所有内容,我不知道更改字符串@句子开头和结尾的语法是否正确。请让我知道我做错了什么!谢谢!

【问题讨论】:

    标签: regex string perl file-io


    【解决方案1】:

    我只会使用 perl 单行代码

    perl -i.bak -pe 's/^[搜索]/[替换]/g;' [files] 用于行首 perl -i.bak -pe 's/[搜索]$/[替换]/g;' [文件] 用于行尾

    [search] 和 [replace] 是占位符。

    “-i.bak”将在替换之前备份文件。

    谷歌快速搜索:perl one-liner search & repalce example

    【讨论】:

      【解决方案2】:

      您已启用use strict;,但使用变量@lines2 时出现语法错误。

      另外,您应该添加use autodie;,因为您正在处理文件。

      您可能正在寻找以下内容:

      my $data = do {
          open my $fh, $file;
          local $/;
          <$fh>
      };
      
      if ($beg eq "y") {
          $data =~ s/(?:^|(?<=\.))\Q$search\E/$replace/gim;
      }
      
      if ($end eq "y") {
          $data =~ s/\Q$search\E(?=\.|$)/$replace/gim;
      }
      
      open my $fh, '>', $file;
      print $fh $data;
      close $fh;
      

      显然,您的代码可能会更加收紧,但以上内容应该可以使其正常工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-23
        • 1970-01-01
        • 1970-01-01
        • 2015-07-01
        • 2012-09-21
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        相关资源
        最近更新 更多