Skip to main content
8 of 12
added 444 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.


As a function with params

Always line eleven is boring with a function "putfirst":

]# declare -f putfirst 
putfirst () 
{ 
    e="1h;2,$(($1-1))H;${1}G;${1},\$p";
    sed -ne "$e" $2
}

Two steps: string generation, then the sed call. "$" has two meanings: "p" is not a variable!

This is the lowest number that works:

]# seq 7 | putfirst 3 
3
1
2
4
5
6
7

Or with the original "bonj" file:

]# putfirst 4 bonj | putfirst 6 |head -4
Latin: Salve 
German: Hallo 
English: Hello 
Turkish: Marhaba 

This is two seds in a row, but now doing two operations.


Perl

perl -ne '$n++; $h.=$_ if $n<11; print $_.$h if $n==11; print if $n>11' <(seq 20)

And as some script:

$lnum=11 ;
while (<>) {
    $n++ ;
    if ($n < $lnum) {
        $sto .= $_ ;
    } else {                                          # lnum is reached
        if ($n == $lnum) { print $_ . $sto  }         # append stored lines
           else          { print $_  }                # just print (neither <lnum nor ==lnum)
    }
}

An if less but a nested else more.

user373503