Description
  • Each line of the datafile is a record consisting of a key field, followed by a colon, and a value.
  • The key field may contain several keys separated by commas.
We want to transform every multikey record to equivalent single-key records by appending a colon and the value to every key.
Raw Input
to,two,too:t u
their,there:th
that:tha
think,thing:th ng
Desired Output
to:t u
two:t u
too:t u
their:th
there:th
that:tha
think:th ng
thing:th ng
Script and Comments
Script1
[ 1] s/^([^,]+),([^:]+):(.*)/\1:\3\n\2:\3/
[ 2] P
[ 3] D
Comments
  1. The `-r' option of GNU sed must be used, or you have to escape every parenthesis and the `+' quantifier.
  2. The Pattern Space is abbreviated to PS.
  3. Take a line to,two,too:t u as an example:
    • The line was read to PS before Step [1].
    • Step [1] transforms the PS to to:t u\ntwo,too:t u. The PS now has two lines in it.
    • Command `P' of Step [2] prints the first line of the PS: to:t u.
    • Step [3] deletes the first line of the PS, resulting in two,too:t u. Then sed jumps to Step [1].