how to evaluate number between a range of numbers using different variables in bash
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have many pairs of integers like (a,b),(c,d),(e,f) .. so on. I need to check if the value of 1st element lies between a range of 90% to 150% of the 2nd element and print the value as Pass.
The values (a,b) are from different columns of a file for two different times respectively.
File1:1 12 123 1234-text-1 12345
File2:2 23 234 1239-text-1 12347
a="$(find . -name *abc.gz* -mtime 1 | xargs zcat | awk -F"," 'print $4' | grep "text-1" | awk '$1=$1;print' | awk 'print $1')"
b="$(find . -name *abc.gz* -mtime 10 | xargs zcat | awk -F"," 'print $4' | | grep "text-1"| awk '$1=$1;print' | awk 'print $1')"
#a=1234
#b=1239
b1="$(bc <<<"$b*9/10")"
b2="$(bc <<<"$b*15/10")"
if [ $a -ge $b1 -a $a -le $b2 ];
then
echo "PASS"
else
echo "FAIL"
fi
I need to do it for different text values like 'text-2', 'text-3' etc for different columns of a file $5, $6 etc.
Please guide me a single iterative solution in Bash.
bash shell-script
add a comment |Â
up vote
0
down vote
favorite
I have many pairs of integers like (a,b),(c,d),(e,f) .. so on. I need to check if the value of 1st element lies between a range of 90% to 150% of the 2nd element and print the value as Pass.
The values (a,b) are from different columns of a file for two different times respectively.
File1:1 12 123 1234-text-1 12345
File2:2 23 234 1239-text-1 12347
a="$(find . -name *abc.gz* -mtime 1 | xargs zcat | awk -F"," 'print $4' | grep "text-1" | awk '$1=$1;print' | awk 'print $1')"
b="$(find . -name *abc.gz* -mtime 10 | xargs zcat | awk -F"," 'print $4' | | grep "text-1"| awk '$1=$1;print' | awk 'print $1')"
#a=1234
#b=1239
b1="$(bc <<<"$b*9/10")"
b2="$(bc <<<"$b*15/10")"
if [ $a -ge $b1 -a $a -le $b2 ];
then
echo "PASS"
else
echo "FAIL"
fi
I need to do it for different text values like 'text-2', 'text-3' etc for different columns of a file $5, $6 etc.
Please guide me a single iterative solution in Bash.
bash shell-script
1
Are these from different files? Your question is really unclear: please simplify it.
â glenn jackman
Jun 21 at 15:36
@Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column usingcut
instead ofawk
. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.
â user1934428
Jun 22 at 5:09
@Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that$1=$1
is a no-op: You just assign the first field to itself. What's the purpose of this?
â user1934428
Jun 22 at 5:12
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have many pairs of integers like (a,b),(c,d),(e,f) .. so on. I need to check if the value of 1st element lies between a range of 90% to 150% of the 2nd element and print the value as Pass.
The values (a,b) are from different columns of a file for two different times respectively.
File1:1 12 123 1234-text-1 12345
File2:2 23 234 1239-text-1 12347
a="$(find . -name *abc.gz* -mtime 1 | xargs zcat | awk -F"," 'print $4' | grep "text-1" | awk '$1=$1;print' | awk 'print $1')"
b="$(find . -name *abc.gz* -mtime 10 | xargs zcat | awk -F"," 'print $4' | | grep "text-1"| awk '$1=$1;print' | awk 'print $1')"
#a=1234
#b=1239
b1="$(bc <<<"$b*9/10")"
b2="$(bc <<<"$b*15/10")"
if [ $a -ge $b1 -a $a -le $b2 ];
then
echo "PASS"
else
echo "FAIL"
fi
I need to do it for different text values like 'text-2', 'text-3' etc for different columns of a file $5, $6 etc.
Please guide me a single iterative solution in Bash.
bash shell-script
I have many pairs of integers like (a,b),(c,d),(e,f) .. so on. I need to check if the value of 1st element lies between a range of 90% to 150% of the 2nd element and print the value as Pass.
The values (a,b) are from different columns of a file for two different times respectively.
File1:1 12 123 1234-text-1 12345
File2:2 23 234 1239-text-1 12347
a="$(find . -name *abc.gz* -mtime 1 | xargs zcat | awk -F"," 'print $4' | grep "text-1" | awk '$1=$1;print' | awk 'print $1')"
b="$(find . -name *abc.gz* -mtime 10 | xargs zcat | awk -F"," 'print $4' | | grep "text-1"| awk '$1=$1;print' | awk 'print $1')"
#a=1234
#b=1239
b1="$(bc <<<"$b*9/10")"
b2="$(bc <<<"$b*15/10")"
if [ $a -ge $b1 -a $a -le $b2 ];
then
echo "PASS"
else
echo "FAIL"
fi
I need to do it for different text values like 'text-2', 'text-3' etc for different columns of a file $5, $6 etc.
Please guide me a single iterative solution in Bash.
bash shell-script
edited Jun 21 at 9:56
asked Jun 21 at 9:31
Varun
11
11
1
Are these from different files? Your question is really unclear: please simplify it.
â glenn jackman
Jun 21 at 15:36
@Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column usingcut
instead ofawk
. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.
â user1934428
Jun 22 at 5:09
@Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that$1=$1
is a no-op: You just assign the first field to itself. What's the purpose of this?
â user1934428
Jun 22 at 5:12
add a comment |Â
1
Are these from different files? Your question is really unclear: please simplify it.
â glenn jackman
Jun 21 at 15:36
@Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column usingcut
instead ofawk
. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.
â user1934428
Jun 22 at 5:09
@Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that$1=$1
is a no-op: You just assign the first field to itself. What's the purpose of this?
â user1934428
Jun 22 at 5:12
1
1
Are these from different files? Your question is really unclear: please simplify it.
â glenn jackman
Jun 21 at 15:36
Are these from different files? Your question is really unclear: please simplify it.
â glenn jackman
Jun 21 at 15:36
@Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column using
cut
instead of awk
. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.â user1934428
Jun 22 at 5:09
@Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column using
cut
instead of awk
. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.â user1934428
Jun 22 at 5:09
@Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that
$1=$1
is a no-op: You just assign the first field to itself. What's the purpose of this?â user1934428
Jun 22 at 5:12
@Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that
$1=$1
is a no-op: You just assign the first field to itself. What's the purpose of this?â user1934428
Jun 22 at 5:12
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:
$ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
bash: [: 1115.1: integer expression expected
nope
You can have bc do the comparisons:
check()
check 1234 1239 # => OK
check 1234 12390 # => NO
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:
$ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
bash: [: 1115.1: integer expression expected
nope
You can have bc do the comparisons:
check()
check 1234 1239 # => OK
check 1234 12390 # => NO
add a comment |Â
up vote
0
down vote
This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:
$ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
bash: [: 1115.1: integer expression expected
nope
You can have bc do the comparisons:
check()
check 1234 1239 # => OK
check 1234 12390 # => NO
add a comment |Â
up vote
0
down vote
up vote
0
down vote
This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:
$ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
bash: [: 1115.1: integer expression expected
nope
You can have bc do the comparisons:
check()
check 1234 1239 # => OK
check 1234 12390 # => NO
This doesn't answer your whole question, but note that bash cannot do any sort of floating point arithmetic:
$ if [ 1234 -ge 1115.1 -a 1234 -le 1858.5 ]; then echo ok; else echo nope; fi
bash: [: 1115.1: integer expression expected
nope
You can have bc do the comparisons:
check()
check 1234 1239 # => OK
check 1234 12390 # => NO
answered Jun 21 at 15:44
glenn jackman
45.6k265100
45.6k265100
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451055%2fhow-to-evaluate-number-between-a-range-of-numbers-using-different-variables-in-b%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Are these from different files? Your question is really unclear: please simplify it.
â glenn jackman
Jun 21 at 15:36
@Varun : You could pass the text values and column number as a parameter. BTW, it's slightly simpler to extract the column using
cut
instead ofawk
. Also, since you need to spawn so many processes for this simple task, you might consider writing the whole program in a more efficient programming language, such as Perl, Ruby or Python.â user1934428
Jun 22 at 5:09
@Varun : Your approach will fail, if your (unnecessarily complicated) pipe returns more than one value. If you can not exclude this possibility with certainity, I suggest that you check for this. Also note that
$1=$1
is a no-op: You just assign the first field to itself. What's the purpose of this?â user1934428
Jun 22 at 5:12