简体   繁体   中英

count the number of occurrences in a files for particular column using awk

I have one file which has the below input

1,1_2_34_45.csv,2345
2,1_2_34_45.csv,2345
3,1_2_34_45.csv,2345
4,1_2_34_46.csv,2346
5,1_2_34_47.csv,2345

for that i need the following out put below

1_2_34_45.csv,2345,3
1_2_34_46.csv,2346,1
1_2_34_47.csv,2345,1

I have tried the below code

 awk -F , '{a[$2]++ }END{for(i in a){print i,a[i]}}' table.txt > count.txt

but it prints only count,$2 value but the other column details doesnt show up as desired output please help me

Storing the values you want in the array key might be sufficient.

$ awk -F, '{a[$2 FS $3]++} END {for(i in a){print i,a[i]}}' OFS=, input.txt
1_2_34_47.csv,2345,1
1_2_34_46.csv,2346,1
1_2_34_45.csv,2345,3

Note that with an awk script this simple, the output order cannot be guaranteed. (That is, array order is not guaranteed.) If you want to control the order, you'd be best to use an additional array:

$ awk -F, '{k=$2 FS $3} !a[k]++{o[i++]=k} END {for(j=0;j<i;j++){print o[j],a[o[j]]}}' OFS=, input.txt
1_2_34_45.csv,2345,3
1_2_34_46.csv,2346,1
1_2_34_47.csv,2345,1

The second array has an incrementing key that we can step through using a for loop as a counter. The counter preserves the original order of "new" keys in the input stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM