简体   繁体   English

Perl:如何在文件中插入行?

[英]Perl:How to insert line in a file?

I am reading from two files.我正在阅读两个文件。 I'm trying to insert a line from file2 to file1 whenever column 1 contents matches.每当第 1 列内容匹配时,我都会尝试从 file2 到 file1 插入一行。

##FILE1 ##文件1

1   wr  5769        78670002    fqefq  
3   wr  5769        78650003    hfhhg  
5   wr  5769        88990001    dfdsv

##FILE2 ##文件2

1   Step1  
3   Step3  
5   Step5

Desired Output:所需的 Output:

1   wr  5769        78670002    fqefq  
Step1
3   wr  5769        78650003    hfhhg
Step3  
5   wr  5769        88990001    dfdsv
Step5

Code tried:代码尝试:

my $rk="rk.log";
open(my $tt, "<$rk" ) or die "Could not open file $trk: $!";
    while (<$tt>) {
       if ($_ =~ /^(\d+)\s+wr\s+5769\s+(\w+)\s+\.*/gm) { 
          open(p1,"<$temp1") or die "Could not open file $temp1: $!"; 
              while (my $newl = <p1>) {
                   my @fs1 = split " ", $newl;
                   if ($fs1[0] eq $1){
                      print "@fs1\n";
                      print "step   $2\n";
                      } else {
                      print "@fs1\n";
                   }
             }
        }
  }
close p1;
close $tt;

Above code doesn't giving the desired output.上面的代码没有给出所需的 output。 Can anyone suggest me better way to do it?任何人都可以建议我更好的方法吗?

Update ##FILE2更新##FILE2

2   Step1  
4   Step3  
6   Step5

Hopefully, a bit of pseudocode will be enough to get you on the right track.希望一点伪代码足以让您走上正轨。

  • Read file2 into a hash (where the key is the integer and the value is the whole line)将file2读入hash(其中key为integer,value为整行)
  • Open file1打开文件1
  • Read file1 a line at a time一次读取 file1 一行
    • Print the line from file1打印 file1 中的行
    • Extract the integer from the start of the line from line1从 line1 的行首提取 integer
    • If that integer exists in your hash如果 integer 存在于您的 hash
      • Print the line from file2打印 file2 中的行

I believe the simplest method would be to import the two files into separate strings, then create a loop which:我相信最简单的方法是将这两个文件导入单独的字符串,然后创建一个循环:

  1. Finds match in file 1. (Include line breaks in your matches)在文件 1 中查找匹配项。(在匹配项中包含换行符)
  2. Appends match to third string.将匹配附加到第三个字符串。
  3. Deletes match from file 1. (Replace with nothing)从文件 1 中删除匹配项。(替换为空)
  4. Finds match from file 2. (Include line breaks in your matches)从文件 2 中查找匹配项。(在匹配项中包含换行符)
  5. Appends match to third string.将匹配附加到第三个字符串。
  6. Deletes match from file 2.从文件 2 中删除匹配项。

This way you will sequentially order all of your matches from the two files into a string that you can export as a file.这样,您将按顺序将两个文件中的所有匹配项排序为一个可以导出为文件的字符串。

This is works for me:这对我有用:

use Tie::File;

my $fle1 = $ARGV[0]; my $fle2 = $ARGV[1];

open(FL2, $fle2) || die "Couldn't read file $fle2\: $!\n";
my $flecnt2 = do {  local $/; <FL2>; };
close(FL2);

my @array;
tie @array, 'Tie::File', $fle1 || die "Error: Couldn't read and write \"$fle1\" file: $!";
my $str = join "\n", @array;

$str=~s#^([^\s]+)\s(.+)$# my $fulcnt=$&;

    if($flecnt2=~m/^$1\s+(.+)$/m)
    {
        $fulcnt .= "\n$&";
    }
    ($fulcnt);
    #egm;

@array = split/\n/, $str;
untie @array;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM