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, which takes a file name and needs no option:
$want=11 ;
while (<>) {
$n++ ;
if ($n < $want) # before $want: store line
{ $lowlines .= $_ ;
next } # next line (avoids 'else')
if ($n == $want) # at line $want: append stored lines to $_
{ $_ .= $lowlines }
print ; # print $_ for $n not less than $want
}
AWK (stolen from Ed (not the editor!))
NR < 11 {
buf[NR] = $0; next
}
NR >=11 {
print
if (NR == 11) {
for (i=1; i<11; i++) { print buf[i] }
}
}
I used NR instead of incrementing n, and made the flow more explicit. Same "trick": the next simplifies downstream.
With perl -n
$n++ ;
$tmp = $tmp . $_ if $n < 11 ;
print $_ . $tmp if $n == 11 ;
print $_ if $n > 11 ;
This is the best format. Symmetrical.