Reputation: 47
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
Upvotes: 1
Views: 546
Reputation: 36370
idk why i cannot get the second element even if i type fields=2
When you are providing more than 1 argument to print
GNU AWK
does join that arguments using output field separator (OFS
) which by default is space, yet you informed cut
that it should treat ,
as delimiter.
If you must use awk
output piped into cut
then make sure that output field separator of awk
and delimiter of cut
is same, you have basically three options,
awk 'BEGIN{OFS=","}END{print 11,22,33}' emptyfile.txt | cut --delimiter=',' --fields=2
awk 'END{print 11,22,33}' emptyfile.txt | cut --delimiter=' ' --fields=2
awk 'BEGIN{OFS="\t"}END{print 11,22,33}' emptyfile.txt | cut --fields=2
all of them give same output
22
Explanation: 1st option is to explicitly set OFS
and --delimiter
to same character in both commands (,
in example), 2nd option is to set --delimiter
to default value of OFS
which is space, 3rd option is to set OFS
to default value of --delimiter
which is TAB character.
Warning in most cases there is no need to pipe awk
output to cut
as task might be done entirely in awk
.
Upvotes: 0
Reputation: 9733
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
Upvotes: 1