简体   繁体   English

使用AWK在大于x的第二列中找到最小数字

[英]Using AWK find a smallest number in a second column bigger than x

I have a file with two columns, 我有一个有两列的文件,

sdfsd 1.3
sdfds 3
sdfsdf 2.1
dsfsdf -1 

if x is 2 如果x是2

I want to print sdfsdf 2.1 我想打印sdfsdf 2.1

How to express it in awk (bash or sed is fine too) 如何在awk中表达它(bash或sed也很好)

It's awfully tempting to do this: 这样做非常诱人:

sort -k 2 -g  | awk '$2 >= 2 { print; exit }'

Tested and works on your example. 测试并适用于您的示例。 If no second column is at least 2, it prints nothing. 如果没有第二列至少为2,则不打印任何内容。

awk: AWK:

BEGIN {
  min=0
  mint=""
  threshold=2
}
{
  if($2 > threshold && ($2 < min || min == 0)) {
    min = $2
    mint = $1
  }
}
END
{
  print mint, min
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM