Bubnoff
Bubnoff

Reputation: 4097

Bash/Linux: Merge rows on match; add last field

I have a set of wireless stats from various branches in the organization:

branchA,171
branchA_guests,1020
branchB,2019
branchB_guests,3409

There are 2 entries for each branch: 1st is internal wifi usage, the next is guest usage. I'd like to merge them into a single total as we don't care whether it's guests or staff ...etc.

Desired output should be:

branchA,1191
branchB,5428

The input file has a header and some markdown so it has to identify a match, not just assume the next line is related --- though the data could be cleaned first, it is my opinion that a match would make this more bulletproof.

Upvotes: 0

Views: 39

Answers (1)

Hai Vu
Hai Vu

Reputation: 40723

Here is my approach: Remove the _guests and tally:

# file: tally.awk
BEGIN {
    FS = OFS = ","
}

{
    sub(/_guests/, "", $1)  # Remove _guests 
    stat[$1] += $2          # Tally
}

END {
    for (branch in stat) {
        printf "%s,%d\n", branch, stat[branch]
    }
}

Running the script:

awk -f tally.awk data.txt

Notes

  • In the BEGIN pattern, I set the field separator (FS) and output field separator (OFS) both to a comma
  • Next, for each line, I remove the _guests part and tally the count
  • Finally, at the end of the file, I print out the counts

Upvotes: 2

Related Questions