one-liner: print all lines except the last 3?

后端 未结 10 1213
灰色年华
灰色年华 2021-01-18 01:46

I would like to simulate GNU\'s head -n -3, which prints all lines except the last 3, because head on FreeBSD doesn\'t have this feature. So I am t

10条回答
  •  暖寄归人
    2021-01-18 01:55

    seq 1 10 | perl -e '@x=("")x3;while(<>){print shift @x;push @x,$_}'
    

    or

    perl -e '@x=("")x3;while(<>){print shift @x;push @x,$_}' file
    

    or

    command | perl -pe 'BEGIN{@x=("")x3}push @x,$_;$_=shift @x'
    perl -pe 'BEGIN{@x=("")x3}push @x,$_;$_=shift @x' file
    

提交回复
热议问题