1

The cmd:

STATUS=`grep word a.log | tail -1 | awk '{print $1,$2,$7,$8,$9}'`
echo "$STATUS"

The output:

2020-05-18 09:27:01 1 of 122

I need to display this $STATUS and need to do the test comparison as well. How to compare number 122 in below? How to represent 122 in $X? The number 122 can be any number, resulted from above cmd.

 if [ "$X" -gt "300" ]
 then
  echo "$STATUS. This in HIGH queue ($X)"
 else
  echo "$STATUS. This is NORMAL ($X)"
 fi

2 Answers 2

3

You could do it with one awk script:

awk '
  /word/{ status=$1" "$2" "$7" "$8" "$9; x=$9 } 
  END{ printf status". This %s (%s)\n", (x>300 ? "in HIGH queue" : "is NORMAL"), x }
' a.log
1
  • Thanks Freddy. I tried this 'shortcut' cmd and it works!. This is another level of expertise.
    – tuti
    Commented May 19, 2020 at 4:12
1

I would suggest using lowercase for variables to reduce possible confusion for someone other than the original author reading the script in the future. Also using $() is typically preferable to using back-ticks -- makes quoting easier to get right.

status="$(grep word a.log | tail -1 | awk '{print $1,$2,$7,$8,$9}')"
x="$(printf '%s' "$status" | awk '{ print $NF }')"
if [ "$x" -gt 300 ]
then
    echo "$status. This in HIGH queue ($x)"
else
    echo "$status. This is NORMAL ($x)"
fi

Note -- we could refactor the status line a bit:

status="$(awk '/word/ { x = $1 OFS $2 OFS $7 OFS $8 OFS $9 } END { print x }' a.log)"

5
  • Thank you Michael Back. This solve my problem. Noted on the lower case and also $().
    – tuti
    Commented May 19, 2020 at 3:49
  • @MichaelBack : I would suggest using x=${status#* of } instead, which makes the pipe and the awk child process unnecessary. Commented May 19, 2020 at 7:46
  • @user1934428 - nice! I'd have to think about it though... because I expect that $9 is always going to be the number he is looking for, but... is $8 always going to be "of?" Commented May 19, 2020 at 10:12
  • @MichaelBack : Unfortunately the OP did not clearly specify the pattern, so technically, we should not have given an answer, but close the question as being "unclear". Of course we can do an educated guess based on the data provided, and if the OP thinks that this does not fit, he can ask a new question. Commented May 19, 2020 at 10:53
  • 1
    @user1934428 - in any human endeavor, there is always going to be some ambiguity... As the Good Book says, "In the multitude of words there wanteth not sin." So, I'm happy that tuti had fun learning something, and he (or she) is off scripting better than before. :) Commented May 19, 2020 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.