Delete specific line number(s) from a text file using sed?

Justin Ethier picture Justin Ethier · Jan 21, 2010 · Viewed 272.2k times · Source

I want to delete one or more specific line numbers from a file. How would I do this using sed?

Answer

Brian Campbell picture Brian Campbell · Jan 21, 2010

If you want to delete lines 5 through 10 and 12:

sed -e '5,10d;12d' file

This will print the results to the screen. If you want to save the results to the same file:

sed -i.bak -e '5,10d;12d' file

This will back the file up to file.bak, and delete the given lines.

Note: Line numbers start at 1. The first line of the file is 1, not 0.