loveclassic
loveclassic

Reputation: 25

Comparing two variables in AWK

I have one issue regarding the comparison of two variables using AWK in LINUX. For example, with a=090810 and b=090910, it works fine using shell:

if [ $a -le $b ]; then
 echo "Hello"
fi

However, it does not work using awk:

if ( a -le b ) print "Hello"

if ( a < b ) print "Hello"

but this works fine:

if ( a < "090910" ) print "Hello"

Can anyone help me solving this problem?

Upvotes: 1

Views: 5204

Answers (2)

James Brown
James Brown

Reputation: 37404

There is no -le operator in awk, use <, >, <=, >=:

$ awk 'BEGIN {a=090810; b=090910; if(a<b) print "a<b"; if(a>b) print "a>b"}'
a<b

Upvotes: 0

Guru
Guru

Reputation: 16984

You need to pass the variables to awk from the shell using the -v option:

$ x=10
$ y=5
$ echo | awk -v x="$x" -v y="$y" '{print x+y}' 
15

Upvotes: 3

Related Questions