Perl: How do I remove the first line of a file without reading and copying whole file

前端 未结 6 1250
刺人心
刺人心 2021-01-01 19:06

I do have a whole bunch of files in a directory and from every file I want to remove the first line (including carriage return). I can read the whole file into an array of s

相关标签:
6条回答
  • 2021-01-01 19:39
    perl -n -i -e 'print unless $. == 1' myfile
    

    This is similar to stocherilac's answer.

    But, in any case (and in all the others answer given!) you are always reading the full file. No way of avoiding that, AFAIK.

    0 讨论(0)
  • 2021-01-01 19:41

    Oh the prefered language is Perl.

    Sometimes sed is a better sed than even perl:

    sed -i 1d *
    
    0 讨论(0)
  • 2021-01-01 19:43

    How about

    tail +2
    

    in shell?

    (edit: in newer Linux you may need tail -n +2 (thank you, GNU! :( ))

    0 讨论(0)
  • 2021-01-01 19:43
    use Tie::File qw();
    for my $filename (glob 'some_where/some_files*') {
        tie my @file, 'Tie::File', $filename or die "Could not open $filename: $!";
        shift @file;
    }
    
    0 讨论(0)
  • 2021-01-01 19:43

    As pointed out by Schwern, the following does not perform an early exit as I had originally thought it would:

    perl -pi -e '$_ = q// and last if $. == 1;' myFile
    

    Seems like one cannot avoid processing the whole file after all.

    0 讨论(0)
  • 2021-01-01 19:58

    Try this one liner

    perl -pi -e '$_ = "" if ( $. == 1 );' filename
    

    I've used it before, should be all you need.

    0 讨论(0)
提交回复
热议问题