Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Perl regex/replace中使用变量?_Regex_Perl_Variables_Replace - Fatal编程技术网

如何在Perl regex/replace中使用变量?

如何在Perl regex/replace中使用变量?,regex,perl,variables,replace,Regex,Perl,Variables,Replace,基本上,我试图在文本文件中插入一行,并生成一个系统命令来执行perl-pi.orig-e 以下是我想做的: my $find='# Foo Bar' my $replace="foo/bar/blah.txt\n# Foo' my $file = './some_text_file.txt' system("perl -pi.orig -e 's|$find|$replace|;' $file"); 这在不使用变量的情况下工作 $file中包含以下文本: # Foo Bar is a co

基本上,我试图在文本文件中插入一行,并生成一个系统命令来执行perl-pi.orig-e

以下是我想做的:

my $find='# Foo Bar'
my $replace="foo/bar/blah.txt\n# Foo'

my $file = './some_text_file.txt'

system("perl -pi.orig -e 's|$find|$replace|;' $file");
这在不使用变量的情况下工作

$file中包含以下文本:

# Foo Bar is a comment
then/a/path/to/a/file.txt

不要从Perl调用Perl,直接在一个进程中完成工作

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

my $find    = qr/# Foo/;
my $replace = "foo/bar/blah.txt\n# Foo";
my $file    = './some_text_file.txt';

open my $in, '<', $file or die $!;
open my $out, '>', "$file.new" or die $!;

while (<$in>) {
    s/$find/$replace/;
    print {$out} $_;
}
close $out;
rename "$file.new", $file or die "Can't rename";

不要从Perl调用Perl,直接在一个进程中完成工作

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

my $find    = qr/# Foo/;
my $replace = "foo/bar/blah.txt\n# Foo";
my $file    = './some_text_file.txt';

open my $in, '<', $file or die $!;
open my $out, '>', "$file.new" or die $!;

while (<$in>) {
    s/$find/$replace/;
    print {$out} $_;
}
close $out;
rename "$file.new", $file or die "Can't rename";

我们还可以使用简单的正则表达式,并将其替换为我们希望的任何内容:

测验
我们还可以使用简单的正则表达式,并将其替换为我们希望的任何内容:

测验
当您有一个要在程序中使用的单行程序时,请查看需要执行的操作:

$ perl -MO=Deparse -pi.orig -e 's|$find|$replace|;' file
BEGIN { $^I = ".orig"; }
LINE: while (defined($_ = readline ARGV)) {
    s/$find/$replace/;
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK
从那里你可以看到你需要做什么


在没有执行文件处理的特殊变量$^I的情况下执行相同的操作。

当您有一个要在程序中使用的单行程序时,要查看需要执行的操作:

$ perl -MO=Deparse -pi.orig -e 's|$find|$replace|;' file
BEGIN { $^I = ".orig"; }
LINE: while (defined($_ = readline ARGV)) {
    s/$find/$replace/;
}
continue {
    die "-p destination: $!\n" unless print $_;
}
-e syntax OK
从那里你可以看到你需要做什么


在没有执行文件处理的特殊变量$^I的情况下执行相同的操作。

如果我首先更正语法错误,例如缺少/错误的引号和缺少分号,则效果很好。请提供您正在使用的输入文件,以便我们可以尝试重新生成。如果我首先更正语法错误,例如缺少/错误的引号和缺少分号,则该文件对我来说效果良好。请提供您正在使用的输入文件,以便我们可以尝试复制他们的代码试图在文件的每一行上运行该正则表达式并更改文件。系统运行一个外部命令,他们要运行的命令是一个perl单行程序perl-pie“…”文件,它在适当的位置更改文件。他们的代码试图在文件的每一行上运行该正则表达式并更改文件。系统运行一个外部命令,他们要运行的命令是一个perl单行程序perl-pie“…”文件,它会在适当的位置更改一个文件请参见@PrashantKulkarni:你说的“不工作”是什么意思?这对我有用。对不起,我太快按了回车键。这是我的调试器输出:DB x$find=qr/Foo Bar/0 Regexp=REGEXP0x5c56eb8->qr/?^u:Foo Bar/DB x$replace=include Foo/Bar/blah.txt\n Foo Bar 0'include Foo/Bar/blah.txt Foo Bar'DB x open my$in,$file.new或die$!0 1 DB x而{s/$find/$replace/;print{$out}$\u;}0 DB x close$out;0 DB x重命名$matchingfile.new、$matchingfile或die$!;01@PrashantKulkarni:你运行的东西与我发布的不同$matchingfile和$file是不同的变量。请注意,我的脚本是兼容的。对不起,这只是我评论中的一个输入错误。在我的代码里是一样的文件没有modified@PrashantKulkarni当前位置你说没有工作是什么意思?这对我有用。对不起,我太快按了回车键。这是我的调试器输出:DB x$find=qr/Foo Bar/0 Regexp=REGEXP0x5c56eb8->qr/?^u:Foo Bar/DB x$replace=include Foo/Bar/blah.txt\n Foo Bar 0'include Foo/Bar/blah.txt Foo Bar'DB x open my$in,$file.new或die$!0 1 DB x而{s/$find/$replace/;print{$out}$\u;}0 DB x close$out;0 DB x重命名$matchingfile.new、$matchingfile或die$!;01@PrashantKulkarni:你运行的东西与我发布的不同$matchingfile和$file是不同的变量。请注意,我的脚本是兼容的。对不起,这只是我评论中的一个输入错误。在我的代码中是一样的文件不会被修改