How do i add horizontally??

1.04/1.05:ELA=20000,POLLK=35000,RH=5000,MH=7000,WH=4359
1.7:ELA=2000,POLLK=2000,RH=2000,MH=2000,WH=607
1.9:ELA=2000,POLLK=2000,RH=2000,MH=2000,WH=396
2.0:POLLK=6000,WENS=10000,ELA=7789

[/CODE]

I have this above content where in i wan the total of the every row

i.e.

ELA=20000,POLLK=35000,RH=5000,MH=7000,WH=4359

[/CODE]

20000+35000+5000+7000+4359
....
....
...
similarly for other rows how can i do plz helpppp??

perl -lne '$sum = 0; while(/=([0-9]*)/g) { $sum += $1 }; print $sum' file

Try awk -F'[,=]' '{for (i=2;i<=NF;i+=2) {s=s+$i;} print s;}' file

hey balajesuri

I wanted individual rows total not the total of all the rows

for ex....

20000+35000+5000+7000+4359=71359

[/CODE]

---------- Post updated at 04:13 PM ---------- Previous update was at 04:11 PM ----------

the o/p is wrong pikk45...
moreover i wanted individual row's total please note

I tried

echo "1.04/1.05ELA=20000,POLLK=35000,RH=5000,MH=7000,WH=4359
1.7:ELA=2000,POLLK=2000,RH=2000,MH=2000,WH=607
1.9:ELA=2000,POLLK=2000,RH=2000,MH=2000,WH=396
2.0:POLLK=6000,WENS=10000,ELA=7789" | awk -F'[,=]' '{for (i=2;i<=NF;i+=2) {s=s+$i;} print s; s=0; }'

and got

71359
8607
8396
23789

Missed out setting s to zero back again :slight_smile: sorry!! This should do!

20000+35000+5000+7000+4359=71359
2000+2000+2000+2000+607=8607
....
....
this way it goes on for othe rows m ok if i oly get the total i.e. 71359 and 8607 etc

---------- Post updated at 04:16 PM ---------- Previous update was at 04:14 PM ----------

71359
8607
8396
23789

this is what i want

---------- Post updated at 04:18 PM ---------- Previous update was at 04:16 PM ----------

thanks pikk...
it worked.... :slight_smile:

---------- Post updated at 04:25 PM ---------- Previous update was at 04:18 PM ----------

is it possible to get o/p this way???

1.04/1.05=	71359
1.7=	8607
1.9=	8396
2.0=	23789

[/CODE]

---------- Post updated at 04:44 PM ---------- Previous update was at 04:25 PM ----------

1.04/1.05=71359
1.7=8607
1.9=8396
2.0=23789

[/CODE][/code]

with this... I modified a little :slight_smile:

awk -F'[:,=]' '{c=$1; for (i=3;i<=NF;i+=2) {s=s+$i;} print c"="s;}' file

This is the output I got:
71359
8607
8396
23789

Did you try the perl one-liner? What output did you get?

This can be shorten some, by removing not needed ; , and simplify the sum

awk -F'[:,=]' '{c=$1; for (i=3;i<=NF;i+=2) {s+=$i} print c"="s}' file

or like this

awk -F'[:,=]' '{c=$1; for (i=3;i<=NF;i+=2) s+=$i; print c"="s}' file

Thanks Jotne. :slight_smile: So it should be,

awk -F'[:,=]' '{c=$1; for (i=3;i<=NF;i+=2) s+=$i; print c"="s; s=0}' file