首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl:如果找到匹配,如何插入一行?

Perl:如果找到匹配,如何插入一行?
EN

Stack Overflow用户
提问于 2021-01-20 08:05:05
回答 2查看 73关注 0票数 1

文件内容:

代码语言:javascript
复制
456 name1 name2 345 678
423 name3 name4 345 678
435 name5 name6 345 678
866 name7 name8 345 678

插入行文件内容后的

代码语言:javascript
复制
456 name1 name2 345 678
423 name3 name4 345 678
Name3_Found
435 name5 name6 345 678
866 name7 name8 345 678

我得到的文件内容:

代码语言:javascript
复制
456 name1 name2 345 678
423 name3 name4 345 678
Name3_Found
me6 345 678
866 name7 name8 345 678

代码语言:javascript
复制
open (temp2, "+<$file") or die "Could not open file";
my $point;
   
while(my $lin =<temp2>) {
    $point = tell(temp2);
    if ( $lin =~ /name5/ ){
        seek(temp2,$point-2,0);
        chk: 
        while (my $lin =<temp2>){
            my @rw = split" ",$lin;
            if ($rw[1] eq "name3"  ) {
                say temp2 "Name3_Found";
            } elsif( $lin =~ /name5/){
                last chk;
            }
        }
    }
}
close temp2;

有人知道为什么要删除其他行数据吗?以及如何修复它?

EN

回答 2

Stack Overflow用户

发布于 2021-01-20 11:03:58

你在这里混了几件事。当您写入混合模式文件句柄(+<)时,您不会插入数据.你在覆盖已经存在的东西。

如果您有固定长度的记录,并且希望访问特定的记录,请使用seek。要替换记录,您必须写出完全相同数量的八位数。如果您编写的八进制数太少,则会留下前一条记录中的八位字节,如果您写的过多,则会覆盖下一条记录。

如果你想做面向行的事情,不要做混合模式的open。打开文件读取它,并输出到一个新文件。perlfaq5在“如何更改、删除或插入文件中的一行,或如何附加到文件的开头”中介绍了这一点?

我认为学习Perl,第4版在这方面还有一个章节。

票数 2
EN

Stack Overflow用户

发布于 2021-01-20 15:31:47

仅仅是因为你问,“.以及如何修复它?”

代码语言:javascript
复制
#!/usr/bin/env perl

use Data::Dumper;
use Hash::Util qw(lock_keys);
use strict;
use warnings;

my $file=q{TheData.txt};

# using three argument open and dying with error
open (my $temp, "+<",$file)
    or die "Could not open '$file'! $!";
my %ptr_h=(READER=>tell $temp,WRITER=>tell $temp);
# The only keys we're going to use are READER and WRITER --- anything will error out
lock_keys(%ptr_h);

# Our buffer
my @buffer_a;
while(my $line=<$temp>) {
    # The next read will start here
    $ptr_h{READER}=tell($temp);

    # Make any changes to the line - pushing them onto the buffer
    if ($line =~ m{name3}) { # only if name3
        # pushing them onto the buffer
        push @buffer_a,$line.qq{Name3_Found\n};
        }
    else { # otherwise just save it!
        push @buffer_a,$line;
        }
    # end of changes

    # Do we have anything to write and the room to write it
    while (@buffer_a && length($buffer_a[0]) <= $ptr_h{READER}-$ptr_h{WRITER}) {
        # We do have room to write the first record of the buffer
        seek $temp,$ptr_h{WRITER},0;
        # Enough room to write $Buffer_a[0] so write it ...
        print $temp shift(@buffer_a);
        $ptr_h{WRITER}=tell($temp);
        };
    }
# Go back to where we were reading
continue {
    seek $temp,$ptr_h{READER},0;
    };
# We've read and processed all of the file
# ... now write out what remains of the buffer
seek $temp,$ptr_h{WRITER},0;
while (@buffer_a) {
    print $temp shift(@buffer_a);
    };
# ... and truncate the file
truncate $temp,tell($temp);
# ... and close
close($temp)
    or die "Can't close '$file'! $!";

艾奇通:如果在你的更新过程中发生了什么事情,你可能会“在没有划桨的情况下”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65805370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档