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)
- We remove the special characters
()+
usinggsub()
function. - We iterate over all fields adding variables to an array and adding the values
- We iterate over the array, printing them in our desired format.
- Add a new line after we are done printing
- Delete the array so that we can re-use it on next line
- Note: The order of lines will be same as input file but using the
in
operator for ourfor loop
the variables on each line may appear in random order.