你如何在一列最接近a的数字中找到5个数字$VariableNumber?
例如,如果$VariableNumber= 30则:
示例输入文件:
50
100
70
40
20
10
65
41
92
Run Code Online (Sandbox Code Playgroud)
示例输出:
20
40
41
10
50
Run Code Online (Sandbox Code Playgroud)
有一个答案,有人在之前发布的其他地方找到了特定行中特定列中与给定值最接近的数字匹配,如下所示:
awk -v col_num="3" -v value="$Number" '
func abs(x) { return (x<0) ? -x : x }
{
distance = abs($col_num - value)
}
NR==1 || distance<shortest_distance {
shortest_distance = distance
nearest_value = $col_num
}
END {
print nearest_value
}
'
Run Code Online (Sandbox Code Playgroud)
但我无法适应它