[英]Search a pattern and replace the entire line of a perl module file
I have a perl module file. 我有一个perl模块文件。 like this : 像这样 :
$release_name = 'Software Release';
$primary_version = '1';
$secondary_version = 'R00.0';
$Main_version = 'R00.0';
I want to search the Main_version and replace the line to 我想搜索Main_version并将其替换为
$Main_version = R00.1
when i run the script. 当我运行脚本时。 I have tried like this. 我已经尝试过了 but its not working. 但它不起作用。
#!/usr/bin/perl -w
use strict;
my $base;
my $file = "/main-dir/work/Myfile.pm";
open(FILE, $file) || die "File not found";
my @base = <FILE>;
close(FILE);
my $item = '$Main_version';
my newitem="R00.1";
foreach $base(@base)
{
if($base =~ /$item/){
$base =~ s/$item/$item='$newitem'/gi;
print ("Hello, world!\n");
}
#else { print $base;}
}
open (BASE, ">$file");
print BASE @base;
close (BASE);
How to search and change the entire line of a perl module? 如何搜索和更改perl模块的整行? Thanks. 谢谢。
Had you used the standard VERSION variable, you could have profited from perl-reversion . 如果您使用了标准VERSION变量,则可以从perl-reversion中受益。
The dollar sign is special in regular expressions, it means "the end of the line". 美元符号在正则表达式中很特殊,它表示“行尾”。 Backslash it, or use quotemeta which could be shortened to \\Q
in a regex: 反斜杠,或使用在正则表达式中可以缩短为\\Q
quotemeta :
$base =~ /\Q$item/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.