简体   繁体   English

如何在 Perl 中搜索和替换文件中的字符串

[英]How to search and replace string in a file in Perl

The content of my input file is shown below:我的输入文件的内容如下所示:

abc\**def\ghi**\abc\!!!!!
abc\**4nfiug\frgrefd\gtefe\wf4fs**\abc\df3gwddw
abc\**eg4/refw**\abc\f3

I need to replace whatever string in between abc \\ --------------\\abc in my input file with ABC\\CBA .我需要用ABC\\CBA替换输入文件中abc \\ --------------\\abc之间的任何字符串。

I have tried something like below to get the strings that need to be replaced.我尝试过类似下面的方法来获取需要替换的字符串。 But I get stuck when I need to use the search and replace:但是当我需要使用搜索和替换时,我卡住了:

my $string1 = qr/abc\W+([^a]+)/;
my $string2 = map{/$string1/ => " "} @input_file; # The string that needs to be replaced
my $string3 = 'ABC\CBA'  # String in that. I want it to replace to

s/$string2/$string3/g

How can I fix this?我怎样才能解决这个问题?

perl -i -pe 's/this/that/g;'  file1

A one-liner to fix a file:修复文件的单行:

perl -plwe 's/abc\\\K.*(?=\\abc)/ABC\\CBA/' input.txt > output.txt

Or as a script:或者作为脚本:

use strict;
use warnings;

while (<DATA>) {
    s/abc\\\K.*(?=\\abc)/ABC\\CBA/;
    print;
}

__DATA__
abc\**def\ghi**\abc\!!!!!
abc\**4nfiug\frgrefd\gtefe\wf4fs**\abc\df3gwddw
abc\**eg4/refw**\abc\f3

The \\K (keep) escape sequence means these characters will not be removed. \\K (保持)转义序列意味着这些字符不会被删除。 Similarly, the look-ahead assertion (?= ... ) will keep that part of the match.同样,前瞻断言(?= ... )将保留匹配的那部分。 I assumed you only wanted to change the characters in between.我假设您只想更改两者之间的字符。

Instead of \\K one can use a look-behind assertion: (?<=abc\\\\) .可以使用后视断言代替\\K(?<=abc\\\\) As a personal preference, I used \\K instead.作为个人偏好,我使用了\\K代替。

If you do not want the substitution to operate on the default variable $_ , you have to use the =~ operator:如果您不希望替换对默认变量$_进行操作,则必须使用=~运算符:

#!/usr/bin/perl
use warnings;
use strict;

my @input_file = split /\n/, <<'__EOF__';
abc\**def\ghi**\abc\!!!!!
abc\**4nfiug\frgrefd\gtefe\wf4fs**\abc\df3gwddw
abc\**eg4/refw**\abc\f3
__EOF__

my $pattern = qr/abc\\.*\\abc/;       # pattern to be matched
my $string2 = join "\n", @input_file; # the string that need to be replaced
my $string3 = 'ABC\CBA';              # string i that i want it to replace to

$string2 =~ s/$pattern/$string3/g;
print $string2;
#!/usr/bin/perl
use strict;
use warnings;

open my $fh,"<", "tryit.txt" or die $!;

while (my $line = <$fh>) {
    $line =~ s/(abc\\)(.*?)(\\abc)/$1ABC\\CBA$3/;
    print $line;
}

gives the following with the input data.给出以下输入数据。

abc\ABC\CBA\abc\!!!!!
abc\ABC\CBA\abc\df3gwddw
abc\ABC\CBA\abc\f3

To address your comment about replacing text "inplace" in the file directly, you can use the -i switch for a one-liner.要解决您关于直接替换文件中“就地”文本的评论,您可以使用-i开关进行单行。 In a script, you can perhaps look at using Tie::File , which allows read-write access to lines of a file as (mutable) elements in an array.在脚本中,您或许可以考虑使用Tie::File ,它允许将文件行作为数组中的(可变)元素进行读写访问。 To copy Mike/TLP's answer:复制 Mike/TLP 的回答:

#!/usr/bin/perl
use strict;
use warnings;

use Tie::File;

tie my @file, "Tie::File", "tryit.txt" or die $!;

# I think you have to use $_ here (done implicitly)
while (@file) {
    s/(abc\\)(.*?)(\\abc)/$1ABC\\CBA$3/;
    print;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM