Description
Given a datafile where
- Each line is of the form Key = Value.
- There may be several records with a given key
- If there are records of the same key, for example,
Key = Value1
Key = Value2
Key = Value3,
we want to combine these records to
Key = Value1, Value2, Value3 by listing these values in one line,
separating them by commas.
These values are listed in the same order as how they appear in that
file.
|
| Raw Input
|
| Desired Output
| key1=a1
key3=c1
key2=b1
key1=a2
key2=b2
key4=d1
key4=d2
key3=c2
key2=b3
key3=c3
key1=a3
key4=d3
|
| key1=a1,a2,a3
key3=c1,c2,c3
key2=b1,b2,b3
key4=d1,d2,d3
|
|
Script and Comments
Script1 [ 1] G
[ 2] s/^([^=]+=)([^\n]*)((\n[^\n]*)*\1[^\n]*)/\3,\2/
[ 3] t br0
[ 4] s/^([^\n]*)\n(.*)/\2\n\1/
[ 5] :br0
[ 6] s/^\n//
[ 7] $q
[ 8] x
[ 9] d
| |
|