从一个文件复制选定的行,并将这些行插入选定行后的另一个文件中(Copy selected lines from one file and insert those lines in another f

编程入门 行业动态 更新时间:2024-10-27 00:22:36
从一个文件复制选定的行,并将这些行插入选定行后的另一个文件中(Copy selected lines from one file and insert those lines in another file after selected line)

这里是我的问题让我们说我有一个文件file1.txt与内容:

abc.. def.. ghi.. def..

和第二个文件file2.txt的内容:

xxx.. yyy.. zzz..

现在我想将file1.txt以“def”开头的所有行复制到file2.txt并在file2.txt “yyy ...”行file2.txt

预期产出:

xxx... yyy... def... def... zzz...

我是perl的新手,我已经尝试为此编写简单的代码,但最终的输出只能追加到文件末尾

#!/usr/local/bin/perl -w use strict; use warnings; use vars qw($filecontent $total); my $file1 = "file1.txt"; open(FILE1, $file1) || die "couldn't open the file!"; open(FILE2, '>>file2.txt') || die "couldn't open the file!"; while($filecontent = <FILE1>){ if ( $filecontent =~ /def/ ) { chomp($filecontent); print FILE2 $filecontent ."\n"; #print FILE2 $filecontent ."\n" if $filecontent =~/yyy/; } } close (FILE1); close (FILE2);

perl程序的输出是

xxx... yyy... zzz... def... def...

Here is my question let's say I have a file file1.txt with contents:

abc.. def.. ghi.. def..

and second file file2.txt with contents:

xxx.. yyy.. zzz..

Now I want to copy all the line starting with "def" in file1.txt to file2.txt and append after "yyy..." line in file2.txt

Expected Output:

xxx... yyy... def... def... zzz...

I am pretty much new to perl, I've tried writing simple code for this but end up with output only appending at the end of file

#!/usr/local/bin/perl -w use strict; use warnings; use vars qw($filecontent $total); my $file1 = "file1.txt"; open(FILE1, $file1) || die "couldn't open the file!"; open(FILE2, '>>file2.txt') || die "couldn't open the file!"; while($filecontent = <FILE1>){ if ( $filecontent =~ /def/ ) { chomp($filecontent); print FILE2 $filecontent ."\n"; #print FILE2 $filecontent ."\n" if $filecontent =~/yyy/; } } close (FILE1); close (FILE2);

output of the perl program is

xxx... yyy... zzz... def... def...

最满意答案

我会使用临时文件。

从FILE2中读取并打印所有行(至temp),直到您点击“yyy” 从FILE1读取并打印所有“def”行(至temp) 读取并打印(温度)FILE2的其余部分 将临时文件重命名为FILE2
use strict; 
use warnings;

my $file1 = "file1.txt";
my $file2 = "file2.txt";
my $file3 = "file3.txt";

open(FILE1, '<', $file1) or die "couldn't open the file!";
open(FILE2, '<', $file2) or die "couldn't open the file!";
open(FILE3, '>', $file3) or die "couldn't open temp file";

while (<FILE2>) {
  print FILE3;
  if (/^yyy/) {
    last;
  }
}

while (<FILE1>) {
  if (/^def/) {
    print FILE3;
  }
}

while (<FILE2>) {
  print FILE3;
}

close (FILE1); 
close (FILE2);
close (FILE3);

rename($file3, $file2) or die "unable to rename temp file";

I'd use a temp file.

Read and print all lines (to temp) from FILE2 until you hit "yyy" Read and print all "def" lines (to temp) from FILE1 Read and print (to temp) the rest of FILE2 Rename the temp file to FILE2
use strict; 
use warnings;

my $file1 = "file1.txt";
my $file2 = "file2.txt";
my $file3 = "file3.txt";

open(FILE1, '<', $file1) or die "couldn't open the file!";
open(FILE2, '<', $file2) or die "couldn't open the file!";
open(FILE3, '>', $file3) or die "couldn't open temp file";

while (<FILE2>) {
  print FILE3;
  if (/^yyy/) {
    last;
  }
}

while (<FILE1>) {
  if (/^def/) {
    print FILE3;
  }
}

while (<FILE2>) {
  print FILE3;
}

close (FILE1); 
close (FILE2);
close (FILE3);

rename($file3, $file2) or die "unable to rename temp file";

                    
                     
          

更多推荐

本文发布于:2023-08-03 13:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390812.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   并将   Copy   selected   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!