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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to assign binary values to data above/below a certain threshold?
# 1  
Old 11-28-2017
Question 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:

Code:
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
# 2  
Old 11-28-2017
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.
Code:
awk '{$2=($2<n)?0:1}1'

where n is the desired threshold (e.g. 0.5, 100, or whatever).
These 2 Users Gave Thanks to Scott For This Post:
# 3  
Old 11-28-2017
Quote:
Originally Posted by Scott
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.
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to assign awk values to shell variable?

Hi Gurus, I have a script which assign awk output to shell variable. current it uses two awk command to assign value to two variables. I want to use one command to assign two values to two variables. I tried the code, but it does't work. kindly provide your suggestion. current code... (2 Replies)
Discussion started by: green_k
2 Replies

2. Shell Programming and Scripting

Assign Unknown Values to Variables

i have a program that spits out a certain number of values. i dont know the number of values. they can be 4, 10, 7, 20, no idea. but, i want to be able to assign each of the value returned by this program to a variable. in the latest instance, the program gave the following 6 values: 4... (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

Awk: Iterate over all records, stop when value < threshold

Hello I have data arranged in two columns 0 7.7272086460791485 1 7.560656885151559 2 7.019966124180658 3 7.2403229620576015 4 7.200882072174212 5 7.3231008505909445 6 7.284184600697482 7 6.933936488650265 8 7.311824413328238 9 7.651279125577341 10 7.536873675349084 11... (19 Replies)
Discussion started by: chrisjorg
19 Replies

4. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

5. Shell Programming and Scripting

AWK, Perl or Shell? Unique strings and their maximum values from 3 column data file

I have a file containing data like so: 2012-01-02 GREEN 4 2012-01-02 GREEN 6 2012-01-02 GREEN 7 2012-01-02 BLUE 4 2012-01-02 BLUE 3 2012-01-02 GREEN 4 2012-01-02 RED 4 2012-01-02 RED 8 2012-01-02 GREEN 4 2012-01-02 YELLOW 5 2012-01-02 YELLOW 2 I can't always predict what the... (4 Replies)
Discussion started by: rich@ardz
4 Replies

6. UNIX for Dummies Questions & Answers

using awk iteratively in a script to assign variable values

I have a log file that has certain fields that I want to evaluate, and depending on the value in those fields, I want to put the value of a different field in that line in a particular variable that I'll use later on down the log file. Sort of like setting a switch to change what I do with a bunch... (5 Replies)
Discussion started by: pts2
5 Replies

7. Shell Programming and Scripting

Assign values to variable

Hi Masters, I want to assign the values of one variable to another variable. Here the varaible name 'var' is dynamic. I know the values of V_2 and U_3, but If the i/p of TYPE is 'U' and the NO is 3, then I want to assign the values of U_3 to var. How we can achieve it? TYPE="U"... (4 Replies)
Discussion started by: ecearund
4 Replies

8. Shell Programming and Scripting

assign values from awk output - help

Dear All, I have a command which gives the number of fields of each line of a comma-separated file. sthng like this : cat QDB_20071126_002.bad | awk -F"," '{ print NF }' I need to assign the first output and the last output of the above command to variables in a script. Need help to do... (4 Replies)
Discussion started by: KrishnaSaran
4 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below оооооооооооо0о-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies

10. UNIX for Advanced & Expert Users

Convert Binary data to ascii data

Friends, I've tried on solaris, but I could n't get ascii data dd if=binaryinputfile bs=1 skip=3800 count=4 | od -t u4 output : INDBU3:/usr/users/FTAMUSER/kk $ dd if=SMP20041006173649188151 bs=1 skip=3800 count=4 | od -t u4 4+0 records in 4+0 records out 0000000 0000000000 0000004... (4 Replies)
Discussion started by: krishna
4 Replies
Login or Register to Ask a Question