Question

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?

Était-ce utile?

La solution

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 ()+ using gsub() 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 our for loop the variables on each line may appear in random order.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top