Description
Each line of the datafile is a list consisting of
  • the name of the list, followed by a colon (`:'),
  • members of the list, separated by commas.
  • For example, in D:A,F,C, `A', `F', and `C' are members of a list named `D'.
Now we want to print one per line every member of a list, followed by the name of the list. Lists without members are discarded.
Raw Input Desired Output
D:A,F,C
C:H
F:B,E
B:
E:H
H:
A:G
G:B
A:D
F:D
C:D
H:C
B:F
E:F
H:E
G:A
B:G
Script and Comments
Script1
[ 1] s/^([^:]+):([^,]+)(,|$)/\2:\1\n\1:/
[ 2] /\n/P
[ 3] D
Comments
  1. The `-r' option of GNU sed must be used to make it interpret REs as EREs.
  2. The following steps are iterated on a list till there is no more member:
    • move the name of the list to the end of the first member, separate them with a colon.
    • prepend a newline character, the name of the list, and a colon before the second member.
    • now Pattern Space (abbreviated to `PS') contains two lines.
    • print the first physical line of PS.
    • delete the first physical line of PS.
  3. Using D:A,F,C as an example:
    After StepContents of PS
      D:A,F,C
    1 A:D \n D:F,C
    2 A:D \n D:F,C
    3 D:F,C
    1 F:D \n D:C
    3 D:C
    1 C:D \n D:
    3 D:
    1 D: