Add frequency (number of occurrences) to my table of text through awk

前端 未结 2 1721
陌清茗
陌清茗 2021-01-28 19:07

Given this input table:

pac1 xxx 
pac1 yyy
pac1 zzz
pac2 xxx
pac2 uuu
pac3 zzz
pac3 uuu
pac4 zzz

I need to add frequencies to third column like

2条回答
  •  逝去的感伤
    2021-01-28 19:37

    If you want to use awk, then you will want to run through every line, and collect some information using three associative arrays. One to collect the raw data, one to count the instances of column 2 duplication, and one to count the instances of column 3 duplication. Then, have an END { for (item in data_array)} that walks through the data array, splitting the fields to get values to use as indices for the other two arrays and printing each line with the appropriate frequency. Something like:

    awk '{ data[num++] = $0;
           col1[$1]++;
           col2[$2]++
         }
       END { for (i = 0; i < num; i++) {
            split(data[i], field)
            printf "%s %d/%d\n", data[i], col2[field[2]], col1[field[1]]
           }
        }' < input.file
    

    This only requires reading the file once, and can be extended for other columns and counts. The for loop causes the data to be displayed it the same order in which it was collected.

    Look at man awk for information on associative arrays, splitting a string, and for.

提交回复
热议问题