Using awk to assign binary values to data above/below a certain threshold?

Hello,

I have files containing a large amount of values in columns, and I want to simplify the data by making the values binary, i.e. assigning 1/0 for each value above or below a certain threshold.

I am curious to know if it's possible to use awk to do this (or another way in bash). I've tried a bit, and managed to find a way around this for a cut-off at 0.5, by effectively rounding the value up or down with "0 decimals", using:

awk '{$2=sprintf("%.0f",$2)}1' input > output

(my data is in the second column)

However, I don't think this would be the approach if, say, the threshold would be 0.75 and not 0.5. Or in any case I haven't found a way to do it.

Any ideas/hints to help me please? As I am only a beginner in bash scripting, I would be grateful if you could explain your solution!

Thanks a lot for your help
KS

You can simply use an if statement to test if the value is above or below the threshold and set the field to 1 or 0 based on that.

awk '{$2=($2<n)?0:1}1'

where n is the desired threshold (e.g. 0.5, 100, or whatever).

Many thanks Scott. This is exactly what I needed. I didn't know about "?", I'll look it up!
Best,
KS