views:

343

answers:

3

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

+7  A: 

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.

Brian Campbell
Not all unixes have gnu sed with "-i". Don't make the mistake of falling back to "sed cmd file > file", which will wipe out your file.
pra
+1  A: 
$ cat foo
1
2
3
4
5
$ sed -e '2d;4d' foo
1
3
5
$ 
Matthew Slattery
A: 

and awk as well

awk 'NR!~/^(5|10|25)$/' file
ghostdog74