Add substring in a file containing fixed length record.

I am new to awk and writing a script using awk. I have file containing fixed length records, I wish to extract 2 substring(each substring is padded with zeros on left e.g 000000003623) and add each substring respectively for every record in the file to get total sum of respective substring for all the records.I have written a script as below but not getting the output. Any help would be highly appreciated.

Sample record is:
01803000000000012045005020960073220120330080844355718025772507891000000821020965088000000000000000000000000DFE02E05000000000000000000000000D37304F41366904036roaming.skysundi.com mnc005.mcc450.gert 121000000000000000000000000B4878544c0e501690130393039062000409C600000804F4070200000800000000000000000000000000000000000006000000003623

Missing the script? :stuck_out_tongue:

Can you please give some example. It is difficult to understand the problem you are facing from current explanation.

Thanks

Thanks for the reply..

The script:

awk 'BEGIN {sum_a=0;sum_b=0;a=substr($0,389,3);b=substr($0,399,3) }
{
printf"%d\t\t%d",a,b;
sum_a = sum_a+a;
sum_b = sum_b+b;
}
END {printf"%d\t\t%d",sum_a,sum_b}

'

=> The sample fixed length srting in the question is a continuous string containing spaces.

You're attempting to extract the substrings before you've read any records.

awk 'BEGIN {sum_a=0;sum_b=0; }
{
a=substr($0,389,3);b=substr($0,399,3)
printf"%d\t\t%d",a,b;
sum_a = sum_a+a;
sum_b = sum_b+b;
}
END {printf"%d\t\t%d",sum_a,sum_b}'
1 Like

Thanks CarloM it working fine now, thanks arun.ijhs.klm for your time.
can you tell me the best way to debug any shell script in general