Reputation: 43
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
Upvotes: 3
Views: 1372
Reputation: 46896
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.
Upvotes: 2