I want to delete one or more specific line numbers from a file. How would I do this using sed?
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
If you want to delete lines 5 through 10 and 12:
This will print the results to the screen. If you want to save the results to the same file:
This will back the file up to |
|||||||||||||||||||||
|
|
and awk as well
|
|||||||||
|
|
|||
|
|
|
You can delete a particular single line with its line number by sed -i '33d' file This will delete the line on 33 line number and save the updated file. |
|||
|
|
|
This is very often a symptom of an antipattern. The tool which produced the line numbers may well be replaced with one which deletes the lines right away. For example;
(where
Having said that, if you are in a situation where you genuinely need to perform this task, you can generate a simple
This accepts a file of line numbers, one per line, and produces, on standard output, the same line numbers with
On some platforms, As always, you can add |
|||||
|
|
I would like to propose a generalization with awk. When the file is made by blocks of a fixed size and the lines to delete are repeated for each block, awk can work fine in such a way
In this example the size for the block is 2000 and I want to print the lines [1..713] and [1026..1029].
|
|||||
|