I would like to use bash tools to solve the following two related problems.
1. Suppose that I have a file with the following lines
A foo1
B bar
A foo2
A foo3 foo4
C ccc
How could I merge the lines that have the same starting field? In other words, I would like to get the following output:
A foo1 foo2 foo3 foo4
B bar
C ccc
2. After finishing the first job, there may be duplicated fields in each line. For example, foo2 may be identical to foo4. How can I remove the duplications and keep only one of them (i.e., keep only foo2)? This has to be done for each line, but we do not care about duplications across the lines.
Background: You may assume that I am compiling a dictionary. The starting field of each line is a word, and the following fields are its meanings.