Imagine an output of a command like
44444
55555
11111
22222
33333
how can I yank the first N lines (first two in the example above) and append them at the end, but without using temp file (so only using pipes)?
11111
22222
33333
44444
55555
Something along the lines of | sed -n '3,5p;1,2p'
(which obviously doesn't work as sed doesn't care about the order of the commands).
Best Answer
Just copy those lines to the hold buffer (then delete them) and when on last line append the content of hold buffer to pattern space:
With
gnu sed
you could write it asHere's another way with ol'
ed
(itr
eads the output ofsome command
into the text buffer and thenm
oves lines1,NUMBER
after the la$
t one):Note that - as pointed out - these will both fail if the output has less than
NUMBER
+1 lines. A more solid approach would be (gnu sed
syntax):this one only deletes lines in that range as long as they're not the last line (
$!d
) - else it overwrites pattern space with hold buffer content (g
) and thenq
uits (after printing the current pattern space).