bash awk 大于小于但在设定范围内
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19516044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Awk greater than less than but within a set range
提问by dat789
I have a script that basically evaulates 2 decimal numbers.
我有一个基本上评估 2 个十进制数的脚本。
if (( $(echo "$p $q" | awk '{ print ( < )}') )); then
echo "Evaluation: Acceptable!"
q is a decimal or number from user input.
p is a calculated figure.
q 是来自用户输入的小数或数字。
p 是一个计算值。
Consequently, if p=1, and q=2, then the outcome is Acceptable.
因此,如果 p=1 且 q=2,则结果是可接受的。
Question#1
How do we evaulate it to be UNacceptable if the calculated p is -150, while q=2. Basically, if p is less than 0 or a negative value, the outcome should be UNacceptable.
问题#1
如果计算出的 p 为 -150,而 q=2,我们如何将其评估为不可接受的。基本上,如果 p 小于 0 或负值,结果应该是不可接受的。
Question#2
q is a range: -q < 0 < q
Example: User input q=0.01
Acceptable range: -0.01 to 0.01
If p is within this range, then it's acceptable, else UNacceptable.
问题#2
q 是一个范围:-q < 0 < q
示例:用户输入 q=0.01
可接受范围:-0.01 到 0.01
如果 p 在此范围内,则它是可接受的,否则不可接受。
Any ideas?
有任何想法吗?
采纳答案by anubhava
I think this awk should be enough for you:
我认为这个 awk 对你来说应该足够了:
awk '{print ( > 0 && < )}'
About your requirement # 2:
关于您的要求#2:
Since any p cannot be negative as per requirement #1 therefore just checking $1 < $2
is enough for you.
由于根据要求#1,任何 p 都不能为负,因此只需检查$1 < $2
就足够了。
回答by Mark Plotnick
It wasn't clear whether your 2 questions are additional restrictions, to be added to your "if (p < q)" condition, or if they're separate. I'll show you three separate awk invocations; let us know if you need any to be combined. In most cases, you can just add conditions separated by &&
inside the if-condition. Setting variables p and q instead of using $1 and $2 seems clearer to me, but if you're just writing one-liners it doesn't matter much.
目前尚不清楚您的 2 个问题是否是附加限制,要添加到您的“if (p < q)”条件中,还是它们是分开的。我将向您展示三个单独的 awk 调用;如果您需要合并,请告诉我们。在大多数情况下,您可以只&&
在 if 条件中添加由分隔的条件。设置变量 p 和 q 而不是使用 $1 和 $2 对我来说似乎更清楚,但是如果您只是编写单行代码,则没有太大关系。
echo $p $q | awk '{ p=; q=; if (p < q) print "acceptable"; }'
echo $p $q | awk '{ p=; q=; if (p < 150) print "UNacceptable"; }'
echo $p $q | awk '{ p=; q=; if (p >= -q && p <= q) print "acceptable"; else print "UNacceptable"; }'