Perl处理文件,怎么将第一行和最后一行删除掉

2025-04-28 22:50:27
推荐回答(1个)
回答1:

如果文件不大的话,可以把文件读入数组,然后再处理保存到另外一个文件里。

#!/usr/bin/perl -w

use strict;

my @file = `cat /usr/home/cel/sample.txt`;
open FH,">>/usr/home/cel/newfile.txt" or die "Can't open /usr/home/cel/newfile.txt: $!\n";
foreach my $i (0..$#file){
    chomp $file[$i];
    next if $i == 0;
    next if $i == $#file;
    print FH "$file[$i]\n";
}
close FH;