Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk greater than less than but within a set range

I have a script that basically evaulates 2 decimal numbers.

if (( $(echo "$p $q" | awk '{ print ($1 < $2)}') )); then
   echo "Evaluation: Acceptable!"

q is a decimal or number from user input.
p is a calculated figure.

Consequently, if p=1, and q=2, then the outcome is Acceptable.

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.

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.

Any ideas?

like image 680
dat789 Avatar asked Oct 22 '13 11:10

dat789


2 Answers

I think this awk should be enough for you:

awk '{print ($1 > 0 && $1 < $2)}'

About your requirement # 2:

Since any p cannot be negative as per requirement #1 therefore just checking $1 < $2 is enough for you.

like image 186
anubhava Avatar answered Sep 20 '22 04:09

anubhava


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.

echo $p $q | awk '{ p=$1; q=$2; if (p < q) print "acceptable"; }'
echo $p $q | awk '{ p=$1; q=$2; if (p < 150) print "UNacceptable"; }'
echo $p $q | awk '{ p=$1; q=$2; if (p >= -q && p <= q) print "acceptable"; else print "UNacceptable"; }'
like image 42
Mark Plotnick Avatar answered Sep 18 '22 04:09

Mark Plotnick