Gawk not filtering out larger number? Gawk not filtering out larger number? unix unix

Gawk not filtering out larger number?


Your awk command performs lexical comparison rather than numerical comparison, because the RHS - the price value - is enclosed in double-quotes.

Removing the double-quotes would help, but it's advisable to reformulate the command as follows:

 gawk -F '\t+' -v price="$price" '$5 <= price' file

The shell variable $price is now passed to Awk using -v, as Awk variable price, which is the safe way to pass values to awk - you can then use a single-quoted awk script without having to splice in shell variables or having to worry about which parts may be expanded by the shell up front.

Afterthought: As Ed Morton points out in a comment, to ensure that a field or variable is treated as a number, append +0 to it; e.g., $5 <= price+0 (conversely, append "" to force treatment as a string).
By default, Awk infers from the values involved and the context whether to interpret a given value as a string or a number - which may not always give the desired result.


You're really calling a separate gawk for each column? One will do:

gawk -F "\t+" -v OFS="\t"   \    -v city="$city"         \    -v bedrooms="$bedrooms" \    -v space="$space"       \    -v price="$price"       \    -v weeks="$weeks"       '        $2 == city && $3 >= bedrooms && $4 >= space && $5 <= price && $6 <= weeks {            $1 = $1; print        }' listing |sort   -t $'\t' $sortby $ordering |column -s $'\t' -t

(This is not an answer, just a comment that needs formatting)

The $1=$1 bit is an awk trick to make it rewrite the current record using the Output Field Separator, a single tab. Saves you a call to tr