0

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.

3
  • 1
    Are these from different files? Your question is really unclear: please simplify it. Commented Jun 21, 2018 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. Commented Jun 22, 2018 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? Commented Jun 22, 2018 at 5:12

1 Answer 1

0

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() {
   local result=$(echo "0.9*$2 <= $1 && $1 <= 1.5*$2" | bc)
   (( result == 1 )) && echo OK || echo NO
}
check 1234 1239    # => OK
check 1234 12390   # => NO

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .