Issue
I am having a file a.txt from that i grep a line start with A and cut 3 to 7 index character and try to sum them but problem is that it is also having (-) sign how I am able to do that.
I am working on linux server and tried many script but not able to find a solution.
and using this code its doing addition but i also want to do subtraction according to sign
grep "^A" a.txt |cut -c3-7 |awk '{SUM += $1} END {print SUM}'
original file contains:
A05000+
B05005-
C05845+
A05007-
A05648+
Should evaluate like:
5000+ 5007- 5648
, resulting in 4359
.
Expexted output: 5641
getting Output: 15655
Solution
Here is one awk
variation.
awk '/^A/ {s+=substr($1,7,1) substr($1,2,5)} END {print s}' file
5641
+5000+-5007+5648=5641
Answered By - Jotne