Regex 如何用另一个文档中的行替换文档中的通配符?
如何更改通配符,让我们假设此通配符Regex 如何用另一个文档中的行替换文档中的通配符?,regex,perl,replace,Regex,Perl,Replace,如何更改通配符,让我们假设此通配符,并将其更改为其他文档中的第一行 文档A:包含通配符如下: <x> text text text text text text <x> tex text <x> text 因此,期望的输出是: num-1 text text text text text text num-2 tex text num-3 text 我真的什么都没试过,因为我甚至不知道如何开始。 但我可以猜到,从一个文件读到另一个文件是必要的,这很困难
,并将其更改为其他文档中的第一行
文档A:包含通配符如下:
<x> text text text
text text text <x>
tex text <x> text
因此,期望的输出是:
num-1 text text text
text text text num-2
tex text num-3 text
我真的什么都没试过,因为我甚至不知道如何开始。
但我可以猜到,从一个文件读到另一个文件是必要的,这很困难
更新
Miller看一下这个结果:输入与上面提供的相同
text text text
text text text num-2
text xt num-3
读入第二个文件,构建单词列表。然后迭代第一个文件,每次替换一个“通配符”
use strict;
use warnings;
use autodie;
my $file1 = 'foo.txt';
my $file2 = 'bar.txt';
open my $fh, '<', $file2;
chomp(my @words = <$fh>);
close $fh;
open $fh, '<', $file1;
while (<$fh>) {
s/<x>/shift @words/eg;
print;
}
使用严格;
使用警告;
使用自动模具;
my$file1='foo.txt';
my$file2='bar.txt';
打开我的$fh,'嗨,米勒,壳牌给了我这个:one@one-桌面:~/desktop$perl wildmarks.pl words 2 words.4在foo.txt文件中,我有这样一个:words.9行可以做得稍微简洁一些,如chomp(my@words=)代码>是的,在数组中使用chomp
的想法对我来说常常很有趣,所以我不这么认为。尽管如此,updated.miller我还是在问题下方添加了结果,因为在add coment中,它丢失了我复制时的格式,我不明白为什么不起作用,因为g修饰符在那里,应该替换所有的ocurrence.qtax我不知道如何更改它,但我注意到了它。
use strict;
use warnings;
use autodie;
my $file1 = 'foo.txt';
my $file2 = 'bar.txt';
open my $fh, '<', $file2;
chomp(my @words = <$fh>);
close $fh;
open $fh, '<', $file1;
while (<$fh>) {
s/<x>/shift @words/eg;
print;
}