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
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
.