Using awk to print output based on first field.

Hi Folks,

I have one requirement, There is one file, which contains two fields.

Based on first field, I need to print an output.

Example will be more suitable.

Input file like this.

abc 5
abc 10
xyz 6
xyz 9
xyz 10
mnp 10
mnp 12
mnp 6

Output should be like this.

abc 5 10
xyz 6 9 10
mnp 10 12 6

There can be thousands line in the file.

for Unique entry in field one, whatever is the value on field two, should be printed in front of unique field one.

Please help in achieving this requirement.

Thank You.

awk '{a[$1]=($1 in a)?a[$1] OFS $2:$2}END {for (i in a) print i,a}' myFile
1 Like

Thank you for quick response.

I tried this(added ( before i in a).

awk '{a[$1]=($1 in a)?a[$1] OFS $2:$2} END {for (i in a) print i,a}'