Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Only print if the number of field is greater than a value with awk

I’m still a newbie to awk, what am I doing wrong?
apologies for the poor description, I reformulate.

Goal

Only print the number of the second field if the number is > 20

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

lorem v3  <--- no print
ipsum v5  <--- no print
text v21  <--- print "21"
expla v12 <--- no print

My attempt that does not work

awk ' { sub("^v","",$2); if ( $2 > 20 ) print $2 } '

>Solution :

Addressing OP’s question about why the current code outputs 3:

Initially awk doesn’t know if $2 is a number or a string.

The sub() call (a string function) tells awk that $2 is to be treated as a string, which also means $2 will be treated as a string for the rest of the script.

This leads to $2 > 20 being treated as a string comparison ('3' > '20') and since '3' (the string) is ‘greater than’ '20' (the string), a 3 is output.

To facilitate a numeric comparion we need a way to force awk to re-evaluate $2 as a numeric. One method is to add a zero, ie, ( $2+0 > 20 ):

$ echo "lorem v3" | awk ' { sub("^v","",$2); if ( $2+0 > 20 ) print $2 } '
           <<< no output

NOTE: for more details see GNU awk – variable typing

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading