參考文章: 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)