参考文章: http://360537539.blog.51cto.com/3803422/715993
一 .asort,asorti在man中的说明:
asort(s [, d]) Returns the number of elements in the source array s. The contents of s are sorted using gawk's normal rules for comparing values, and the indices of the sorted values of s are replaced with sequential integers starting with 1. If the optional destination array d is specified, then s is first duplicated into d, and then d is sorted, leaving the indices of the source array s unchanged.
asorti(s [, d]) Returns the number of elements in the source array s. The behavior is the same as that of asort(), except that <strong><span style="color:#ff0000;">the array indices are used for sorting</span></strong>, not the array values. When done, the array is indexed numerically, and the values are those of the original indices. The original values are lost; thus pro‐vide a second array if you wish to preserve the original.
使用范围:
asort 对value排序,不能保存key-value的关系(使用遍历在value值唯一的情况下能区分出,建立value-key数组);asort按照数值排序
asorti 对key排序,可以保存key-value的关系(通过key可以找到value,key的值唯一);asorti按照文本排序
说明:
返回值:数组长度(数组s中的元素数量)
排序:对数组的值(value)进行排序(不考虑key)
排序依据:使用gawk的默认比较规则(数字或者字符的大小),数据指针内容被数字代替(从1开始);
如果d被指定,则s不会被修改,改动的内容写入d中;
awk '{a[$1]=$1}END{l=asorti(a,b);for (i=1;i<=l;i++)print a[b[i]]}' 2.txt #2.txt format: num(3,1,7,6,2,9,8)