Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.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
Regex Perl编辑文件_Regex_Perl - Fatal编程技术网

Regex Perl编辑文件

Regex Perl编辑文件,regex,perl,Regex,Perl,我的文件包含100行 1234 ABC 100.0.0.0 4567 DEF 200.0.0.0 ..... 我正在匹配文件中的模式。例如,搜索4567,并将200.0.0.0替换为500.0.0.0,使该行现在看起来4567 DEF 500.0.0 my $file = "$file_path/file.lst"; my $newid = "500.0.0.0" open MAST, $file or die "Unable to open file.lst: $!"; my $new =

我的文件包含100行

1234 ABC 100.0.0.0 
4567 DEF 200.0.0.0 .....
我正在匹配文件中的模式。例如,搜索
4567
,并将
200.0.0.0
替换为
500.0.0.0
,使该行现在看起来
4567 DEF 500.0.0

my $file = "$file_path/file.lst";
my $newid = "500.0.0.0"
open MAST, $file or die "Unable to open file.lst: $!";
my $new = "$file.tmp.$$";
my $bak = "$file.bak";
open(NEW, "> $new")         or die "can't open $new: $!";

while (<MAST>) {
my ($pattern,$id) = (split /\s+/, $_)[0,4];
        print $_;
        if ( $_ =~ m/^$pattern/ ) {
             $_ =~ s/$id/$newid/g;
        }
        (print NEW $_)          or die "can't write to $new: $!";
}

close(MAST)                    or die "can't close $file: $!";
close(NEW)                     or die "can't close $new: $!";

rename($file, $bak)          or die "can't rename $file to $bak: $!";
rename($new, $file)          or die "can't rename $new to $file: $!";
my$file=“$file\u path/file.lst”;
my$newid=“500.0.0.0”
打开MAST,$file或die“无法打开file.lst:$!”;
my$new=“$file.tmp.$$”;
my$bak=“$file.bak”;
打开(新“>$NEW”)或死亡“无法打开$NEW:$!”;
而(){
我的($pattern,$id)=(split/\s+/,$)[0,4];
打印美元;
如果($\uz=~m/^$pattern/){
$\=~s/$id/$newid/g;
}
(打印新$或“无法写入$NEW:$!”;
}
关闭(桅杆)或死亡“无法关闭$file:$!”;
关闭(新建)或死亡“无法关闭$NEW:$!”;
重命名($file,$bak)或“无法将$file重命名为$bak:$!”;
重命名($new,$file)或“无法将$new重命名为$file:$!”;
我需要做什么:
在屏幕上显示更改前和更改后的行,并请求用户确认,然后继续其他操作


请注意。

若您执行代码,它将显示编译错误,因为语句未在第二行终止。
始终使用
使用警告
使用严格在shebang行之后的程序顶部

以下是一种搜索模式并替换的方法:

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

my $file = "file.txt";
my $newid = "500.0.0.0";
my @update;
open my $fh, "<", $file or die $!;

while ( my $line = <$fh> )
{
    my ($pattern, $id) = (split(/\s+/, $line))[0, 2];  #split line to get first and third column value
    if ($pattern =~ m/4567/)
    {   
        print "Before change: $line\n";     
        $line =~ s/$id/$newid/g;  #replace id with newid when pattern is 4567
        print "After change: $line\n";
        #print "Confirm with Yes or No: ";  #here you can for confirmation
        #chomp(my $confirmation = <STDIN>);
    }
    push @update, $line;
}
close $fh;
#modify the file
open my $fhw, ">", $file or die "Couldn't modify file: $!";
print $fhw @update;
close $fhw;


----------file.txt----------
1234 ABC 100.0.0.0
4567 DEF 200.0.0.0
#/usr/bin/perl
使用警告;
严格使用;
my$file=“file.txt”;
my$newid=“500.0.0.0”;
我的@update;
打开我的$fh,“,$file或die”无法修改文件:$!”;
打印$fhw@update;
接近$fhw;
----------file.txt----------
1234 ABC 100.0.0.0
4567 DEF 200.0.0.0

它还将在控制台上打印更改前和更改后的匹配行。您可以根据需要修改此代码。确认部分在您的问题中不是很清楚。

如果没有用户确认元素,则可以:

perl -i.bak -pe 'm/^4567/ and s/200/500/' filename

您是否需要每行用户确认?您可以使用
diff
检查这是否满足了您的需要

你为什么需要确认?这看起来像是一个错误