Reputation: 27
My aim is to run an awk code to check the value of column 4 and assign the count of its occurrence against it. My data looks like this
DN Area Peri FID DN2 Area2 Peri2 FID2
1 70 39 1066 1 47 29 3
1 70 39 1083 1 1142 262 11
1 4662 465 1085 1 23 19 16
1 4662 465 1085 1 47 29 9
1 4662 465 1085 1 1142 262 11
1 280 78 1087 1 513 126 18
1 23 19 1093 1 47 29 12
1 93 48 1094 1 93 48 19
1 117 48 1100 1 466 136 17
1 326 126 1131 1 723 223 54
1 326 126 1131 1 513 126 18
1 23 19 1135 1 723 136 32
1 47 29 1138 1 1702 281 49
1 187 87 1150 1 47 29 40
1 23 19 1153 1 187 78 47
1 2191 397 1156 1 23 19 24
1 2191 397 1156 1 47 29 20
1 2191 397 1156 1 5524 581 87
1 256 87 1157 1 466 136 41
Expected outcome:
1
1
3
3 I
3
1
1
1
1
2
2
1
1
1
1
3
3
3
1
when I run the code I've written I get a '>' and the command doesnt stop running
awk '{if ( FNR>1 && count[$4]=i ) { for(i=1; i<=NR; ++i) printf "%d\n", i}} file
Upvotes: 0
Views: 220
Reputation: 26591
In a single pass awk you can do (with the assumption you do not want to count the header)
awk '{ a[NR]=$4; c[$4]++ }END{for(i=2;i<=NR;++i) print c[a[i]] }' file
Upvotes: 0
Reputation: 17058
Read the file twice: Count the occurences in the first run (NR==FNR
) and print them out in the second run (FNR<NR
).
awk 'FNR==NR{a[$4]++}FNR<NR{print a[$4]}' file file
This counting also includes the headline, skip it if you don't want it.
Upvotes: 1
Reputation: 133780
Could you please try following.
awk 'FNR==NR{a[$4]++;next} FNR!=NR && FNR>1{print $4,a[$4]}' Input_file Input_file
Above will print 4th column and its occurrence value both, in case you need only occurrence value then remove $4
from print
part in above code.
Upvotes: 1