Reputation: 139
I have a bunch of variables which look like this:
(DURATION 1.57) + (DURATION 2.07)
(T0 10) (T1 0) (TX 0) + (T0 12) (T1 0) (TX 1)
(TC 1) (IG 0) + (TC 2) (IG 3)
Is it possible to have awk process this such that the result is:
(DURATION 3.64)
(T0 22) (T1 0) (TX 1)
(TC 3) (IG 3)
Or can anyone recommend another unix program I can use to do this?
Upvotes: 1
Views: 118
Reputation: 77105
Here is one way to do it:
awk '{
gsub(/[()+]/, "")
for(nf=1; nf<=NF; nf+=2) {
flds[$nf] += $(nf+1)
}
sep = ""
for(fld in flds) {
printf "%s(%s %g)", sep, fld, flds[fld]
sep = FS
}
print "";
delete flds
}' file
(DURATION 3.64)
(T0 22) (T1 0) (TX 1)
(TC 3) (IG 3)
()+
using gsub()
function. in
operator for our for loop
the variables on each line may appear in random order. Upvotes: 2