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

前端 未结 22 850
走了就别回头了
走了就别回头了 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:28

    From the sed one-liners:

    # delete the last 10 lines of a file
    sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
    sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2
    

    Seems to be what you are looing for.

提交回复
热议问题