将几行从一个文件复制到另一个文件

时间:2018-02-15 11:43:02

标签: perl file copy

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

abc 
def ->[
    sff 
    ghi 
]
dqq

现在我想将从sff开始的所有行复制到ghi file1.txtfile2.txt

的结尾

即。以下行应添加到file2.txt

sff
ghi

这是我的意思。建议我而不是“最后”我应该用什么来满足我的要求:

My $fh=”file1.txt”;

my @lines = read_file($fh);

while ( my $line = shift @lines) {

      next unless ($line =~ m/def/);

      last; # this code will write till end of file i.e. dqq

}

append_file('file2.txt', @lines);

2 个答案:

答案 0 :(得分:0)

这里是

while(<DATA>){
print $_ if /sff/ ... /ghi/
}


__DATA__
abc 
def ->[
    sff 
    l1
    l2
    ghi 
]
dqq

答案 1 :(得分:0)

我的$ fh1 =&#34; file1.txt&#34 ;;

使用File :: Slurp;

$ search1 =&#34; start&#34 ;;

$ search2 =&#34;结束&#34 ;;

open(WRITE,&#39;&gt;&gt;&#39;,$ new);

打开(fh,&#34;&lt;&#34;,&#34; $ fh1&#34;);

而() {

推@array,$ _ if / $ search1 /../$ search2 /;

}

接近(FH);

append_file(&#39; FILE2.TXT&#39;,@数组);

相关问题