Combine Similar Output from the 2nd field w.r.t 1st Field

Hi,

For example:

I have:

HostA,XYZ
HostB,XYZ
HostC,ABC

I would like the output to be:

HostA,HostB: XYZ
HostC:ABC

How can I achieve this?

So far what I though of is:

  1. Look through 2nd field and
sort 

and

uniq 

it
2. Use what I have from 1. and

grep

the first field.
3. Get the first field I got from

grep

and group them by

comma

I dont really understand your approach, but this

awk -F, '{T[$2] = T[$2] FS $1} END {for (t in T) print substr (T[t],2), t}' OFS=: file
HostA,HostB:XYZ
HostC:ABC

will turn your input sample into the desired output (uncertain if you want spaces in it or not).

1 Like