awk append to one line if first field is the same

Hi all,

How would I append the second field each time to one line if the first field is the same for example I have this data:

10430,187976
10430,251588
10430,262904
10430,275008
10430,279892
10430,275008
10430,303740
10430,318136
10430,336988
10430,350324
10430,373648

And I want the output to be

10430,251588,262904,275008,

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
Are you sure you want the final comma?

awk -F, 'END { for (k in _) print k, _[k] }
{ _[$1] = $1 in _ ? _[$1] FS $2 : $2 }
' infile

Re-reading the post, I'm not sure I understand the requirement ...

Hi radoulov, many thanks for your help, it looks like it is doing what I want.

To explain it, the first column is the process pid, and the 2nd column is the memory use, so I want to append the mem use to the pid so I can graph the data.

Only thing is now the first comma, delimiter seems to get remove when i run this, so i get out put like

10430 187976,251588,262904,275008,279892,292840,303740,318136,336988,350324,373648,395724,412160,422628,431968,438428,440008,442800,445464,448436,448948,449032,452276,457400

I would like to preserve the , after the pid 10430 so when i import it into excel it doesnt think its separate data

radoulov,

This line should have a comma as field separator:

END { for (k in _) print k FS _[k] }

Regards :wink:

Correct! Thank you :slight_smile:
Actually, it could be even:

awk -F, 'END { for (k in _) print _[k] }
{ _[$1] = $1 in _ ? _[$1] FS $2 : $0 }
' infile  

With Perl:

perl -F, -lane'
	push @{ $_{ $F[0] } }, $F[1];
	print map { join ",", $_, @{ $_{$_} } } keys %_
	  if eof
	' infile