ksh field math

Hi,
I have a text like with many rows of data like:

7a,0,1182
7a,1,1040
7b,0,483
7b,1,242
7c,0,224
7c,1,877

I need to be able to take the value of the first field (i.e. 7a) and if there are multiples, add the value of the third field, (1182 + 1040) and insert the output of all of this in a new text file. So the final output would be:

7a,2222
7b,725
7c,1101

This needs to use ksh.
Thanks!!

With awk this is straightforward:

awk '{A[$1]+=$3} END{for(i in A) print i,A}' FS=, OFS=, file

You can call this from ksh .

If you want to do this in ksh only, can you post what you have tried and where you are stuck?
Also what is your OS and version and is this ksh88 or ksh93 ?

1 Like

Also let us know if (as in your example) all occurrences of a given value in field 1 are on contiguous lines in your input text file.

And, please use CODE tags when posting code segments, sample input data, and sample output data instead of depending on moderators to clean up your messages for you.

1 Like