Skip to main content
4 of 5
added 12 characters in body
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

Using ed (the line-editor that sed and grep are derived from):

printf '%s\n' '11m0' 'w bonjour2' 'q' | ed -s bonjour

This applies the editing command 11m0 to the file which moves line 11 to before the first line. It then writes the resulting document to the file bonjour2 and quits.

Alternatively:

printf '%s\n' '11m0' ',p' 'Q' | ed -s bonjour >bonjour2

... which instead of writing to a specific file using ed commands just prints the whole file to standard output. The result is then redirected to a new filename. The ,p command will output the whole document to standard output and Q will force quit (even though the document has changed).

To make the change in the document in-place (i.e. change the original file), write the result back to the file itself:

printf '%s\n' '11m0' 'wq' | ed -s bonjour

Example run of one of the above variations:

$ printf '%s\n' '11m0' ',p' 'Q' | ed -s bonjour >bonjour2
$ cat bonjour2
French: Bonjour
English: Hello
Turkish: Marhaba
Italian: Ciao
German: Hallo
Spanish: Hola
Latin: Salve
Greek: chai-ray
Welsh: Helo
Finnish: Hei
Breton: Demat

To always move the last line to the top, use $m0 in place of 11m0. To always move the line that starts with the string French:, use /^French:/m0.

Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k