awk打印每个类别的所有最小值 [英] awk to print all the minimum values for each category

查看:117
本文介绍了awk打印每个类别的所有最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望根据 $ 1和$ 3 组合打印所有最小值.如果有两行或更多行 对于具有$ 1和$ 3的唯一组合的最小值,则需要打印所有行.例如,具有最小值$ 3-"10"的$ 1-"Abc"出现两次,即Abc,yyy,10,aaa和Abc,ttt,10,aaa.输入文件未按任何顺序排序.

Would like to print all the minimum values based on $1 and $3 combinations. If there are two or more lines are available for minimum value having unique combinations of $1 and $3 then need to print all the lines . For example , $1-"Abc" having minimum value $3-"10" appearing two times i.e Abc,yyy,10,aaa and Abc,ttt,10,aaa. Input file has not sorted on any order.

Input.txt

Input.txt

Country,Desc,Amount,Details
Abc,xxx,20,aaa
Abc,yyy,10,aaa
ghi,ttt,25,ccc
Abc,zzz,35,aaa
def,xxx,30,bbb
Abc,ttt,10,aaa
def,yyy,20,bbb
ghi,yyy,25,ccc
def,zzz,45,bbb
ghi,xxx,35,ccc
ghi,zzz,50,ccc

要与标题行NR==1 , {print}

所需的Output.txt

Desired Output.txt

Country,Desc,Amount,Details
Abc,yyy,10,aaa
Abc,ttt,10,aaa
ghi,ttt,25,ccc
ghi,yyy,25,ccc
def,yyy,20,bbb

我正在使用两个命令来获得所需的输出,首先是sort基于$ 1和$ 3的输入文件,然后是第二个命令awk -F, '!seen[$1]++' 寻找您的建议来简单地喜欢一支班轮.

I am using two commands to get the desired output, first sort the input file based on $1 and $3 then second command awk -F, '!seen[$1]++' Looking for your suggestions to simply like one liner.

推荐答案

一个awk,它将文件处理两次.在第一次运行时,它为每个$ 1选择最小的$ 3,在第二次运行中,它显示最小的:

An awk which processes the file twice. On the first run it picks the smallest $3 for each $1 and on the second it prints the smallest:

$ awk '
BEGIN{FS=","}                             # delimiter
NR==FNR {                                 # first run
    if ($1 in a==0 || $3<a[$1])
        a[$1]=$3
    next
}
$3==a[$1] # || FNR==1                     # if Country is data not header
' file file                               # uncomment the FNR==1
Country,Desc,Amount,Details
Abc,yyy,10,aaa
ghi,ttt,25,ccc
Abc,ttt,10,aaa
def,yyy,20,bbb
ghi,yyy,25,ccc

这篇关于awk打印每个类别的所有最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆