You can use a genuine editor for that, and ed
is the standard editor.
I'm assuming your lines are in a file lines.txt
, one number per line, e.g.,
1
34
45
678
Then (with a blatant bashism):
ed -s file.txt < <(sed -n '/^[[:digit:]]\+$/p' lines.txt | sort -nr | sed 's/$/d/'; printf '%s\n' w q)
A first sed
selects only the numbers from file lines.txt
(just in case).
There's something quite special to take into account here: that when you delete line 1, then line 34 in the original file becomes line 33. So it's better to remove the lines from the end: start with 678, then 45, etc. that's why we're using sort -nr
(to sort the numbers in reverse order). A final sed
appends d
(ed
's delete command) to the numbers.
Then we issue the w
(write) and q
(quit) commands.
Note that this overwrites the original file!