-2

I am echoing some data from an Oracle DB cluster, via a bash script. Currently, my output into a variable in the script from SQLPlus is:

11/12 0 0 0 0 0 0 1 0 1 0 5 4 1 0 0 0 0 0 0 0 0 0 0 0

What I'd like to be able to do is evaluate that string of numbers, excluding the first one (the date), to see if any consecutive 6 of the numbers are above a certain value, lets say 10.

I only want the logic to return true if all 6 consecutive values were above "10".

So for example, if the output was:

11/12 0 0 8 10 5 1 1 0 8 10 25 40 6 2 0 0 0 0 0 0 0 0 0 0

The logic should return false/null/zero, anything I can handle negatively.

But if the string looked like this:

11/12 0 0 0 0 5 9 1 0 1 10 28 10 12 19 15 11 6 7 0 0 0 0

Then it would return true/1 etc..

Is there any bash component that I can make use of to do this? I've been stuck on this part for a while now.

Dave Byrne
  • 429
  • 4
  • 21

2 Answers2

2

Say your string is in $S, then

echo $S | awk '
    { L=0; threshold = 10; reqLength = 6;
      for (i = 2; i <= NF; ++i) {
          if ($i >= threshold) {
              L += 1
              if (L >= reqLength) {
                  exit(1);
              }
          } else {
              L = 0
          }
       }
     }'

would do it. ($? will be 1 if you have enough numbers exceeding your threshold)

Ronald
  • 2,842
  • 16
  • 16
  • 1
    It should be: `i <= NF` and also `L >= regLength`. – PesaThe Dec 11 '17 at 14:35
  • Nice. You don't have to define the threshold and the reqLength for every line. I would pass those values in like this: `awk -v threshold=10 -v reqLength=6 '{L=0; for ...` – glenn jackman Dec 11 '17 at 14:37
  • @PesaThe Thank you! Especially the `i <= NF` is crucial. I edited my answer and used both suggestions – Ronald Dec 11 '17 at 14:37
  • @glennjackman The OP showed only a single line as input. If I'd have to process multiple lines, I'd define the constants in the BEGIN rule. Of course passing them in the command line as you suggest is also OK. – Ronald Dec 11 '17 at 14:40
2

For variety, here is a solution not depending on awk:

#!/usr/bin/env bash

contains() {
   local nums=$* count=0 threshold=10 limit=6 i
   for i in ${nums#* }; do
      if (( i >= threshold )); then
         (( ++count >= limit )) && return 0
      else
         count=0
      fi
   done
   return 1
}

output="11/12 0 0 0 0 5 9 1 0 1 10 28 10 12 19 15 11 6 7 0 0 0 0"
if contains "$output"; then
   echo "Yaaay!"
else
   echo "Noooo!"
fi
PesaThe
  • 7,259
  • 1
  • 19
  • 43