免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 12203 | 回复: 39
打印 上一主题 下一主题

请教用awk求最大值的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-15 14:52 |只看该作者 |倒序浏览
$ cat data
a 1
b 1
c 1
d 1
e 3
f 3
g 1

想用awk打印出其第2个域中最大的行, 也就是
e 3
f 3

论坛徽章:
0
2 [报告]
发表于 2010-03-15 15:05 |只看该作者
本帖最后由 where27 于 2010-03-15 15:10 编辑

回复 1# humanzhang
  1. awk 'NR==FNR{t=(t>$2)?t:$2}NR>FNR&&$NF==t' date date
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-03-15 15:18 |只看该作者

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
4 [报告]
发表于 2010-03-15 15:28 |只看该作者

  1. awk '{a[$0]=$2}END{n=asort(a,b);for (i in a) if (a[i]==b[n]) print i}' urfile
复制代码

论坛徽章:
0
5 [报告]
发表于 2010-03-15 17:24 |只看该作者
回复 4# Shell_HAT


    n=asort(a,b)这是什么意思?

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
6 [报告]
发表于 2010-03-15 17:44 |只看该作者
回复 5# bbgg1983


男人知道:

       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 spec-
                               ified, then s is first duplicated into  d,  and
                               then  d  is  sorted, leaving the indices of the
                               source array s unchanged.

论坛徽章:
0
7 [报告]
发表于 2010-03-15 17:47 |只看该作者
回复 2# where27

真厉害  谢谢 能解释一下你的语法吗

论坛徽章:
0
8 [报告]
发表于 2010-03-15 18:03 |只看该作者
回复 1# humanzhang


    两种办法,一种是 sort -k2nr  文件 | head -1
另外一种是 awk '$2>max{max=$2;maxline=$0}END{print maxline}'  URFIE

论坛徽章:
0
9 [报告]
发表于 2010-03-15 18:07 |只看该作者
回复 8# 709101040516


    谢谢 但是这两种办法都只能得出一个最高值 我想考虑同时又两个最高值的情况

论坛徽章:
0
10 [报告]
发表于 2010-03-15 18:13 |只看该作者
回复 9# humanzhang


    awk 那个 你确定??
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP