简体   繁体   English

BASH:AWK-查找条件为“ if”的最小和最大数字

[英]BASH: AWK - find the smallest and largest number with condition “if”

I've got syntax problem with awk and I don't know how to fix it: 我的awk语法有问题,我不知道如何解决:

 awk -F: -v lim=100 '{if ($1 >= lim)} NR == 1 {line = $0; min = $1} NR > 1 && $1 < min {line = $0; min = $1} END {print min}' file.txt

I want to get printed the smallest number in $1 column but greater than 100. It's works fine but without condition "if". 我想在$ 1列中打印出最小的数字,但大于100。它的工作原理很好,但没有条件“ if”。

Try this awk: 试试这个awk:

awk -F: -v lim=100 '(!min || $1 < min) && $1 >= lim {min=$1} END{print min}' file.txt

If there is no number >= 100 then it will just print 0. 如果没有数字> = 100,那么它将仅打印0。

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

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