VicKy
VicKy

Reputation: 43

Convert and compare grep result with integer

I am trying to compare a result obtained from grep with a integer in unix. But I cannot compare that string to integer. My code goes like this..

set e_pat = `zgrep "count" $file | cut -d'=' -f 2`
if [$e_pat -gt 10000]
  then
      echo "Greater"
else
      echo "lesser"
fi

This code gives error

if: Expression Syntax.

How to convert and compare the grep command result with integer?

Upvotes: 0

Views: 1515

Answers (1)

choroba
choroba

Reputation: 241848

In bash, set is not used to assign values to variables. Instead, use = directly, but without spaces around the assignment operator:

e_pat=`zgrep count "$file" | cut -d= -f2`

It's better to use $() instead of the backticks, as you can nest it without needing to backslash.

e_pat=$(zgrep count "$file" | cut -d= -f2)

Also, quote the variable in comparison, and separate the command [ from it by whitespace; the same applies to the closing ].

if [ "$e_pat" -gt 10000 ]

Upvotes: 1

Related Questions