Quickest way to move a line in a text file before another line in a text file?
A scriptable editor makes this pretty straight-forward!
printf '%s\n' '55m22' 'wq' | ed -s input
This sends two commands to ed
(editing the file named input
):
55m22
-- move line 55 after line 22wq
-- save the file back to disk and quit.
A little shorter:
ex input <<<"55m22|wq"
It's somewhat longer in vi
than in ed
:
vi input
55Gdd23GPZZ
55G
... go to line 55
dd
... delete one line
23G
... go to line 23
P
... paste the deleted line before line 23
ZZ
... write file and exit