Skip to main content
4 of 12
added 692 characters in body
user avatar
user avatar
sed -n '1h;2,10H;11G;11,$p' 

First line, copy h because of new line, then append H until 10.

At line 11, get the hold space

From 11 to end, print.


]#  sed -n '1h;2,10H;11G;11,$p' bonj 
French: Bonjour 
English: Hello 
Turkish: Marhaba 
Italian: Ciao 
German: Hallo 
Spanish: Hola 
Latin: Salve 
Greek: chai-ray 
Welsh: Helo 
Finnish: Hei 
Breton: Demat 

This is nicer:

]# seq 20 | sed -n '1h;2,10H;11G;11,$p' 
11
1
2
3
4
5
6
7
8
9
10
12
13
14
15
16
17
18
19
20

I take your example:

]# sed -e 1p -e 11p -n bonj 
English: Hello 
French: Bonjour 

...with the -n switch at the end just to show it counts for both expressions.

I have also -n, and then 1h;2,10H, which should be just 1,10H, which is a range of line numbers and a "hold" (store) command. Nothing gets printed, yet.

11,$p is another range. On line 11 it prints what `11G' just got back from hold (ie 1-10) and appended to line 11.

Lines 12 until $ just print themselves, because of -n.


I should make two -e like you:

sed -n -e '1h;2,10H' -e '11G;11,$p'

From 1,10 is hold, from 11,$ is print.


Line 11 has the G first, then the p. It matters, because:

]# seq 20 | sed -n -e '1h;2,10H' -e '11,$p;11G'
11
12
13
14
15
16
17
18
19
20

Here line 12 wipes out what line 11 has gotten after printing.

user373503