小编Emm*_*man的帖子

复制一个文件中的选定行,并在选定行后将这些行插入另一个文件中

这是我的问题,假设我有一个file1.txt内容文件 :

abc.. 
def.. 
ghi.. 
def..
Run Code Online (Sandbox Code Playgroud)

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

xxx..
yyy..
zzz..
Run Code Online (Sandbox Code Playgroud)

现在我想复制开始"高清"的所有线file1.txtfile2.txt和追加后"YYY ......"行file2.txt

预期产出:

xxx...
yyy...
def...
def...
zzz...
Run Code Online (Sandbox Code Playgroud)

我对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/; …
Run Code Online (Sandbox Code Playgroud)

perl

3
推荐指数
1
解决办法
6642
查看次数

标签 统计

perl ×1