Description
In the following example, we want to 'move' all the lines containing 'this' to the end of a file, i.e., display lines not containing 'this' first.
Raw Input Desired Output
1 this
2 this
3 another
4 another
5 this
6 another
7 this
8 another
9 this
3 another
4 another
6 another
8 another
1 this
2 this
5 this
7 this
9 this
Script and Comments
Script1
[ 1] /this/H
[ 2] /this/!p
[ 3] $!d
[ 4] x
[ 5] s/^\n//
[ 6] /^$/d
Comments
  1. If a line contains 'this', Step [1] will copy it to the Hold Space; otherwise Step [2] will print it.
  2. Then, if the current line is not the last line of a file, Step [3] will delete it and start next cycle.
  3. After processing the last line (copy it to the Hold Space or not),
    • Step [4] will exchange the contents of the Pattern Space and the Hold Space. Then the PS will have those lines containing 'this'.
    • Step [5] will remove the leading new line character.
    • If the PS is empty, which implies that the file does not have any line containing 'this', without the help of Step [6], you will get a trailing empty line.