Skip to main content
3 of 5
added 41 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 which moves line 11 to before the first line. It writes the resulting document to the file bonjour2 and then 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).

Example:

$ 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