Artificial intelligent assistant

Print all data related to a certain field Considering below file: foo,5 foo,7 foo,9 boo,5 boo,10 boo,10 What I am looking to, is printing all the data in `$2` that is related to `$1` in one record. For this example the needed output will be: foo,5,7,9 boo,5,10

An `awk` solution:


awk 'BEGIN{OFS=FS=","}!(($1,$2)in c){c[$1,$2]=1;r[$1]=r[$1] OFS $2}
END{for(i in r){print i r[i]}}' file


1. Set `OFS=FS` for consistency.
2. Use `awk`'s multidimensional array to remember the `$2` values encountered so far. Only 'proceed' if there is no match (`!(($1,$2)in c)`).
3. Provide a 'dummy' value for the multidimensional array on first encounter of a new key.
4. Concatenate the desired output string into another array for convenience.
5. At the `END`, print the loop.
* Note that the loop iteration is not in any particular order, you can use `awk`'s sorting features if required.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 7424ed89933f7f8d4c3d717556902b0f