简体   繁体   中英

Gawk not filtering out larger number?

My simple gawk filter used in my program is not filtering out a value that is a digit longer than the rest.

Here is my text file:

172 East Fourth Street      Toronto     4   1890    1500000     6
2213 Mt. Vernon Avenue      Vaughn      2   890     500000      4
One Lincoln Plaza           Toronto     2   980     900000      1

The columns are separated by tabs.

My gawk script:

echo "Enter max price"
read price 

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

The 1500000 value appears if I enter a value of 150001 or greater. I think it has to do with the gawk not reading the last digit correctly. I'm not permitted to change the original text file and I need to use the gawk command. Any help is appreciated!

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 ; eg, $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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM