How can I calculate the number of occurences of the second value e.g.:'Rv1408' ? i want to get and calculate the total number of occurrences of the 2nd element in each line.<.br>
file.txt:
Rv0729,Rv1408,Rv1408
Rv0162c,Rv0761,Rv1862,Rv3086
Rv2790c,Rv1408
Rv2fd90c,Rv1408
Rv1862,Rv3086
Rvsf62,Rv3086
i tried(doesnt work) input:
awk ' { tot[$0] } END { for (i in tot) print tot[i],i } ' m.txt | sort | cut --delimiter=',' --fields=1
Expected Output:
total no of occurences:
Rv1408: 3
Rv0761:1
Rv3086: 2
idk why i cannot get the second element even if i type fields=2
CodePudding user response:
You can make it easier by passing the -F comma field separator.
Like this:
awk -F, '{map[$2] } END { for (key in map) { print key, map[key] } }' file.txt