nua7
May 2, 2013, 10:46am
1
Hi All,
I have a file with thousands of lines in the following format, where
Field1=First 8 characters
Field2-9-16 characters
Field3=17-26 characters
I need to add Field 1 and Field2 and subtract the result from Field 3.
Field3=Field3 - (Field1 + Field2)
0012.00 0010.00 0001576.53
0100.00 0500.00 0000952.83
0500.00 0500.00 0001760.74
0000.00 0000.00 0001536.04
0000.00 0000.00 0000389.08
0000.00 0000.00 0000942.09
0000.00 0000.00 0000884.26
Desired output file
0012.00 0010.00 0001554.53
0100.00 0500.00 0000352.83
0500.00 0500.00 0000760.74
0000.00 0000.00 0001536.04
0000.00 0000.00 0000389.08
0000.00 0000.00 0000942.09
0000.00 0000.00 0000884.26
Any help will be appreciated!
Sounds like a task for awk. What have you tried so far? I can see you have plenty of threads opened with questions about similar problems, which have all been answered and solved.
Any post before the OP answers will be moderated.
nua7
May 2, 2013, 12:27pm
3
This is what I have for now.
BEGIN {
out_filel="reports/tst3"
parta=substr($0,1,8)
partb=substr($0,9,8)
totalamt=substr($0,17,10)
totalamt=(totalamt)-(parta + partb) > out_file1
}
And this is how I am calling the awk program.
cat ./reports/tst2|awk -f ./awk_progs/test.awk
But the output file is not getting created.
Try this one
awk '{print $1 " "$2 " " $3-($1+$2)}' file
0012.00 0010.00 1554.53
0100.00 0500.00 352.83
0500.00 0500.00 760.74
0000.00 0000.00 1536.04
0000.00 0000.00 389.08
0000.00 0000.00 942.09
0000.00 0000.00 884.26
1 Like
Yoda
May 2, 2013, 12:42pm
5
To preserve leading zeros in 3rd field:
awk '{$3=sprintf("%010.2f",$3-($1+$2))}1' file