osx - Alternative to uniq -c and sort in pure awk -


here 1 command used filter out access.log file number of hits ip address , count number of hits each ip , sort them lowest highest count:

awk '{print $1}' "${accesslog}" | sort -n | uniq -c | sort -nk1 

and here excerpt result:

 26 45.59.193.115  26 74.125.63.33  27 88.156.36.194  28 12.208.4.156  29 12.208.4.156  31 98.236.117.199  32 176.9.82.6  33 187.34.167.111  35 67.110.83.252  37 54.184.4.183  39 195.59.2.173  39 70.199.109.118  44 12.208.4.156  59 88.156.36.194 

now possible same result use of awk? no uniq -c, no sort.

can't seem find info on web this...

in theory - yes, can. there 2 parts here:

can implement sort , uniq? sort quite tricky, sure, can implement in awk. uniq should trivial.

can implement pipeline, | uniq -c | sort -nk1 | uniq. yes, , it's not going hard. use like:

awk '{ips[$1]++} end {for (ip in ips) { print ips[ip], ip}}' 

that counting / uniq part. you'll have add asort sort entries @ end.


Comments