sed: delete all but the last X lines of a file

5

2

It can be done with other tools, but I am interested to know how can I delete all but the last X lines of the file with sed.

Felipe Alvarez

Posted 2012-09-07T02:10:17.300

Reputation: 1 666

5The hard way. Stick with the other tools. – Ignacio Vazquez-Abrams – 2012-09-07T02:22:30.237

Answers

9

Basically you are emulating tail. X = 20 in this example. The following example will delete all but the last 20 lines:

sed -e :a -e '$q;N;21,$D;ba' filename

Explanation:

  • The -e :a creates a label called a
  • The next -e:
    • $q - quits and prints the pattern space if it is the last line
    • N - next line
    • 21,$D - executes the "D" command if the line# is >= 21 (21,$ = 21st line to $ which is the end of the file)
    • ba - branches to label 'a' which is the beginning of the script.

phiz

Posted 2012-09-07T02:10:17.300

Reputation: 306

@Peter.O That just blew my mind. Could you please explain this bit: '$(($1+1))' ?? – voices – 2016-06-05T22:02:55.650

It works as tail, but I was not able to make it work in place (as the in the original question) – Matteo – 2012-09-07T06:48:06.830

2+1... BTW. It works with -i if you condense the two -e expressions into one. You can also use it by passing bash variable $1 into it... sed -i ':a;$q;N;'$(($1+1))',$D;ba' filename ... but asking for 0 lines, will return 1 line. – Peter.O – 2012-09-07T07:42:20.180

6

sed is quite complex when it comes to task like this one. tail, grep or awk would make this a lot easier and should be used instead. That being said, it is possible.

The following solution is adapted from sed and Multi-Line Search and Replace.

sed -ni '
    # if the first line copy the pattern to the hold buffer
    1h
    # if not the first line then append the pattern to the hold buffer
    1!H
    # if the last line then ...
    ${
            # copy from the hold to the pattern buffer
            g
            # delete current line if it is followed by at least X more lines
            # replace X-1 with the proper value
            s/.*\n\(\(.*\n\)\{X-1\}\)/\1/
            # print
            p
    }
' filename

Without the comments, it makes a nifty one-liner. If you want to eliminate, e.g., everything but the last ten lines, use this:

sed -ni '1h;1!H;${;g;s/.*\n\(\(.*\n\)\{9\}\)/\1/;p;}' filename

Dennis

Posted 2012-09-07T02:10:17.300

Reputation: 42 934

On non-GNU seds you need -e after -i. – Matteo – 2012-09-07T06:46:02.293

Just like phiz's answer, this does not handle a request for 0 last lines... However, that is one of the reasons for the recommendation to use another tool. – Peter.O – 2012-09-07T07:49:38.710

2

Based on the script in section 4.13 of the sed manual you could do something like this:

n=10

(( n > 1 )) && script='1h; 2,'$n'{H;g;}; $q; 1,'$((n-1))'d; N; D'
(( n > 1 )) || script='$!d'

sed -i "$script" infile

Thor

Posted 2012-09-07T02:10:17.300

Reputation: 5 178

0

tac|sed|tac>&&(mv||cat>)

Both of the following command snippets will effectively delete all but the very last 5 lines of ~/file1. If you want to retain the last 10 lines, you can substitute: |sed '1,5!d;' with |sed '1,10!d;', and so on, as you see fit.

  1. tac ~/"file1" |sed '1,5!d;' |tac >"/tmp/file2" &&mv  "/tmp/file2"  ~/"file1"
  2. tac ~/"file1" |sed '1,5!d;' |tac >"/tmp/file2" &&cat "/tmp/file2" >~/"file1"

voices

Posted 2012-09-07T02:10:17.300

Reputation: 2 053