简体   繁体   中英

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 -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".

Try this 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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