awk-排序(asort,asorti)

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值