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

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

    I came up with this, where n is the number of lines you want to delete:

    count=`wc -l file`
    lines=`expr "$count" - n`
    head -n "$lines" file > temp.txt
    mv temp.txt file
    rm -f temp.txt
    

    It's a little roundabout, but I think it's easy to follow.

    1. Count up the number of lines in the main file
    2. Subtract the number of lines you want to remove from the count
    3. Print out the number of lines you want to keep and store in a temp file
    4. Replace the main file with the temp file
    5. Remove the temp file
    0 讨论(0)
  • 2020-11-28 18:44

    If hardcoding n is an option, you can use sequential calls to sed. For instance, to delete the last three lines, delete the last one line thrice:

    sed '$d' file | sed '$d' | sed '$d'
    
    0 讨论(0)
  • 2020-11-28 18:44

    To truncate very large files truly in-place we have truncate command. It doesn't know about lines, but tail + wc can convert lines to bytes:

    file=bigone.log
    lines=3
    truncate -s -$(tail -$lines $file | wc -c) $file
    

    There is an obvious race condition if the file is written at the same time. In this case it may be better to use head - it counts bytes from the beginning of file (mind disk IO), so we will always truncate on line boundary (possibly more lines than expected if file is actively written):

    truncate -s $(head -n -$lines $file | wc -c) $file
    

    Handy one-liner if you fail login attempt putting password in place of username:

    truncate -s $(head -n -5 /var/log/secure | wc -c) /var/log/secure
    
    0 讨论(0)
  • 2020-11-28 18:45

    Just for completeness I would like to add my solution. I ended up doing this with the standard ed:

    ed -s sometextfile <<< $'-2,$d\nwq'
    

    This deletes the last 2 lines using in-place editing (although it does use a temporary file in /tmp !!)

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