[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All,

I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I do not know how to add it up.
Please help me since doing this task manually is really very frustrating.

---------- Post updated at 08:07 PM ---------- Previous update was at 08:06 PM ----------

Im on HP-UX B.11.31

Hard to even guess at a solution without seeing a sample of the text file.
Please include a sample of the file, and remember to enclose it in CodeTags.

can you post a sample of the file and required output ?

Dears,

I have pasted some sample lines and highlighted the duration in bold:

0136530213AN019604609133GC0549465654          blackberry.net          20130219092609000001272               700000000172100130300000000000012120000121242003           000000000        20130219092609M2N8 0518557041297314243           11827013400000084.23.99.130    00000000000000000007000000000000000000000C00000101                                    0
0147200213AN019604509103GC0540246180          web2                    201302191009280000001410567302555     000000000123000000100000000000052700000527042003           000000000        20130219100928M2N8 0518546907805867587           28242021710000084.23.98.130    00000000000000501000000000000000000000000C23230301                                    0
0157650213AN019604509103GC0541471076          web2                    201302190926220000003030563880041     000000000100500000100000000000000000000000042003           000000000        20130219042619M2N8 05164473711405660658          60632070300000084.23.99.52     00000000000000501000000000000000000000000C23220301                                    0
0158110213AN019604609103GC0542371570          web2                    20130219102258000000101               700000010172100140300000010000046380000463842003           000000000        20130219102255M2N8 0518559338163034119           15216013260000084.23.99.134    00000000000000000007000000000000000000000C00000301                                    0
0172350213AN019604609103GC0564271836          web2                    20130219101346000018624               G00000205172100110800000205000002770000027742003           000000000        20130219092837M2N8 0518113891313183537           32174034330000084.23.99.15     00000000000000000104000000000000000000000C00000301                                    0
0173080213AN019604609132GC0568246272          blackberry.net          20130219095540000000001               700000000172100130300000000000010260000102642003           000000000        20130219095540M2N8 0518553727220908279           27201038030000084.23.99.14     00000000000000000007000000000000000000000C00000101                                    0
0188310213AN019604609132GC0569835060          blackberry.net          20130219082549000000133               700000000172100130300000000000013170000131742003           000000000        20130219072549M2N8 0517617595868822730           54733020370000084.23.99.53     00000000000000000007000000000000000000000C00000101                                    0
0189090213AN019604509113GC0548093306          wap2                    201302191012140000000310544554484     000000000120100000100000000000000250000002542003           000000000        20130219101214M2N8 0518555817805883875           53151021360000084.23.98.130    00000000000000501000000000000000000000000C23230101                                    0
awk ' /^[0-9]/{s+=substr($0,85,9)}END{printf "%09d\n", s } ' filename

With code tags, the data looks different! No need for condition, just action:

awk '{s+=substr($0,85,9)}END{printf "%09d\n", s } ' file
1 Like

Thanks a lot..it works great..