簡體   English   中英

搜索模式並替換perl模塊文件的整行

[英]Search a pattern and replace the entire line of a perl module file

我有一個perl模塊文件。 像這樣 :

$release_name       = 'Software Release';
$primary_version    = '1';
$secondary_version  = 'R00.0';
$Main_version   = 'R00.0';

我想搜索Main_version並將其替換為

      $Main_version = R00.1 

當我運行腳本時。 我已經嘗試過了 但它不起作用。

#!/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);

如何搜索和更改perl模塊的整行? 謝謝。

如果您使用了標准VERSION變量,則可以從perl-reversion中受益。

美元符號在正則表達式中很特殊,它表示“行尾”。 反斜杠,或使用在正則表達式中可以縮短為\\Q quotemeta

$base =~ /\Q$item/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM