Mohit Verma
Mohit Verma

Reputation: 2089

Unix: Group by with custom function

My text file looks like this

id, key, value
i1, k1, v1
i1, k2, v2
i1, k2, v3

I need to operate group by on <id, key> and have a custom function which say concats all values. So resulting output looks like

id, key, value
i1, k1, v1
i1, k2, v2||v3

How can we achieve this in unix ?

Upvotes: 0

Views: 49

Answers (1)

Kent
Kent

Reputation: 195169

if you have awk:

awk -F, '{k=$1FS$2;a[k]=(k in a)?a[k]"||"$3:$3}END{for(x in a)print x FS a[x]}' file
id, key, value
i1, k1, v1
i1, k2, v2|| v3

Upvotes: 1

Related Questions