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:
some command | sed '1,NUMBER{ # in this range
H # append line to hold space and
1h # overwrite if it's the 1st line
d # then delete the line
}
$G' # on last line append hold buffer content
With gnu sed
you could write it as
some command | sed '1,NUMBER{H;1h;d;};$G'
Here's another way with ol' ed
(it r
eads the output of some command
into the text buffer and then m
oves lines 1,NUMBER
after the la$
t one):
ed -s <<IN
r ! some command
1,NUMBERm$
,p
q
IN
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):
some command | sed '1,NUMBER{H;1h;$!d;${g;q;};};$G'
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 then q
uits (after printing the current pattern space).