Description
Each line begins with a tag separated with other contents by at least one space. The datafile can be divided into several groups where lines of a group begin with the same tag. The job is printing the last line of every group.
Raw Input Desired Output
John 12
John 13
John 14
Mary 100
Mary 102
Kelly 55
Kelly 56
Kelly 57
Olivier 99
John 14
Mary 102
Kelly 57
Olivier 99
Script and Comments
Script1
[ 1] 1h
[ 2] x
[ 3] G
[ 4] /^\([^ ]*\) .*\n\1 /!P
[ 5] $!d
[ 6] x
Comments
  1. Pattern Space and Hold Space are abbreviated to PS and HS, respectively.
  2. We use the HS to keep the previous line.
  3. Step [1] is used to initialize the HS.
  4. Step [2] is used to exchange the contents of PS and HS.
  5. After Step [3], PS contains
    Previous line \n Current line
  6. In Step [4], if the current and previous line do not begin with the same tag, then command 'P' will print the previous line.
  7. Now HS contains the current line, which can be used in the next cycle.
  8. If the current line is not the last one of datafile, Step [5] will start next cycle; otherwise, the current line will be put back to PS by Step [6] and will be printed when this script is terminated.