return lines containing numbers in a range in a certain column but only if a column contains a certain number

Related to this question... return lines containing numbers in a range in a certain column return lines containing numbers in a range in a certain column

I know how to delete lines which contain numbers in a certain range. Then how to do if I want to delete lines only when the other column contains a number?

For example, I want to delete lines if the second column contains 4, and the third column contains numbers between 50 to 85? input

a 3 44 563
d 4 55 11
e 2 93 445
r 4 82 44
e 4 93 33

output

a 3 44 563
e 2 93 445
e 4 93 33

I think we can do something like this... but this does not work.

awk '\$2==4' & awk '\$2 > 85 && \$2 < 50' input > output

Can anybody know this? Of note, the second column contains only the integers from 1-22.

This awk condition will print those lines:

awk '\$2 != 4 || \$3 > 85 || \$3 < 50' input

It prints a line when the second column does not equal 4, or the third column is greater than 85, or the third column is less than 50.

This logical equivalent should have the same effect:

awk '! (\$2 == 4 && \$3 <= 85 && \$3 >= 50)' input

As noted in comments, a clearer way to write the range comparisons for min <= num <= max would be min <= num && num <= max. So that would be:

awk '! (\$2 == 4 && 50 <= \$3 && \$3 <= 85)' input
awk '\$2 != 4 || \$3 < 50 || 85 < \$3' input
• The common way to write the test for a number in a range is to put the number in the middle with the min value on the left and the max on the right, e.g. min <= num && num <= max, so the layout of your code clearly reflects it's purpose. You CAN obviously write it a bunch of other ways (e.g. as you have num <= max && num >= min) but that way is the clearest to read and hardest to write incorrectly. Mar 28 '21 at 13:37
• Thanks @EdMorton! Should the same apply when testing for a number outside of a range: min > num || num > max? Mar 28 '21 at 17:36
• That's not quite as obvious but num < min || max < num is the syntax that demonstrates that you're testing for num outside of a range as it literally has the word num on the outsides of the condition. So, to test for num inside a range you'd write min num num max and to test for num outside a range you'd write num min max num - the word "num" goes wherever it needs to be relative to the words "min"and "max". Mar 28 '21 at 18:27
• I always put the variable on the left hand side when comparing. Find it much easier to read. For me this for example: var > min && var < max reads as if var is greater then then min and var is less then max. Jun 8 '21 at 4:13