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

后端 未结 10 1192
灰色年华
灰色年华 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 02:00

    Another Awk solution that only uses minimal amount of buffers and prints lines quickly without needing to read all the lines first. It can also be used with pipes and large files.

    awk 'BEGIN{X = 3; for(i = 0; i < X; ++i)getline a[i]}{i %= X; print a[i]; a[i++] = $0}'
    
    0 讨论(0)
  • 2021-01-18 02:02

    how about :

     seq 1 10 | perl -ne 'print if ( !eof  )' | perl -ne 'print if ( !eof  )' | perl -ne 'print if ( !eof  )' 
    
    0 讨论(0)
  • 2021-01-18 02:04

    Pure bash and simple tools (wc and cut):

    head -n $(($(wc -l file | cut -c-8)-3)) file
    

    Disclaimer - I don't have access to FreeBSD right now, but this does work on OSX bash.

    0 讨论(0)
  • 2021-01-18 02:06

    Nobody seems to have use sed and tac, so here's one:

    $ seq 10 | tac | sed '1,3d' | tac
    1
    2
    3
    4
    5
    6
    7
    
    0 讨论(0)
提交回复
热议问题