How to use sed to remove the last n lines of a file

前端 未结 22 849
走了就别回头了
走了就别回头了 2020-11-28 18:12

I want to remove some n lines from the end of a file. Can this be done using sed?

For example, to remove lines from 2 to 4, I can use

$ sed          


        
22条回答
  •  盖世英雄少女心
    2020-11-28 18:22

    With the answers here you'd have already learnt that sed is not the best tool for this application.

    However I do think there is a way to do this in using sed; the idea is to append N lines to hold space untill you are able read without hitting EOF. When EOF is hit, print the contents of hold space and quit.

    sed -e '$!{N;N;N;N;N;N;H;}' -e x
    

    The sed command above will omit last 5 lines.

提交回复
热议问题