Changing values of a column based on formula


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Changing values of a column based on formula
# 1  
Old 10-30-2012
Changing values of a column based on formula

Hi,

I have a tab delimited text file with two columns where the second column takes on values between 1 and 6 (including). If the value of the second column, includes 1,2,3 I want to replace it with LOW. If it is 4,5,6 I want to replace it with HIGH. How do I go about doing that? Thanks

Input:
Code:
A 1
B 4
C 6

Output
Code:
A LOW
B HIGH
C HIGH

# 2  
Old 10-30-2012
Code:
awk '{$2=$2<=3?"LOW":"HIGH"}1' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 11-20-2012
How do I modify the code if I want to add the additional option of replacing it with UNKNOWN if the value equals 1000. Thanks!

Example input:
Code:
A 1
B 4
C 6
D 1000

Example output:
Code:
A LOW
B HIGH
C HIGH
D UNKNOWN

# 4  
Old 11-20-2012
Code:
awk '{$2=$2<=3?"LOW":($2<=6)?"HIGH":"UNKNOWN")}1' file

This User Gave Thanks to vgersh99 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Filtering based on column values

Hi there, I am trying to filter a big file with several columns using values on a column with values like (AC=5;AN=10;SF=341,377,517,643,662;VRT=1). I wont to filter the data based on SF= values that are (bigger than 400) ... (25 Replies)
Discussion started by: daashti
25 Replies

2. Shell Programming and Scripting

Concatenate values in the first column based on the second column.

I have a file (myfile.txt) with contents like this: 1.txt apple is 3.txt apple is 5.txt apple is 2.txt apple is a 7.txt apple is a 8.txt apple is a fruit 4.txt orange not a fruit 6.txt zero isThe above file is already sorted using this command: sort -k2 myfile.txtMy objective is to get... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

3. UNIX for Dummies Questions & Answers

Repositioning based on column values

Dear all ... I have a file which I want to change the structure based on the values in some columns and I would be grateful if you can help... one of my files looks like ... they all have ten rows 1,0,0 10,0,0 2,0,0 3,0,0 4,1,1 4,1,1 4,1,1 5,0,0 6,0,0 7,0,0 8,0.5,2 9,0.33,3 9,0.33,3... (1 Reply)
Discussion started by: A-V
1 Replies

4. Shell Programming and Scripting

Sum column values based in common identifier in 1st column.

Hi, I have a table to be imported for R as matrix or data.frame but I first need to edit it because I've got several lines with the same identifier (1st column), so I want to sum the each column (2nd -nth) of each identifier (1st column) The input is for example, after sorted: K00001 1 1 4 3... (8 Replies)
Discussion started by: sargotrons
8 Replies

5. Shell Programming and Scripting

Changing values only in 3rd column and 4th column

#cat file testing test! nipw asdkjasjdk ok! what !ok host server1 check_ssh_disk!102.56.1.101!30!50!/ other host server 2 des check_ssh_disk!192.6.1.10!40!30!/ #grep check file| awk -F! '{print $3,$4}'|awk '{gsub($1,"",$1)}1' 50 30 # Output: (6 Replies)
Discussion started by: kenshinhimura
6 Replies

6. Shell Programming and Scripting

Changing values in the first column.

I have a file which looks like this: 1 3 5 5 3 2 1 5 3 6 3 2 0 5 6 3 2 9 0 4 5 6 4 9 In the first column, instead of 1, I want to place +1 and instead of 0, I want -1 to be put. So the generated file should look like this: +1 3 5 5 3 2 +1 5 3 6 3 2 -1 5 6 3 2 9 -1 4 5 6 4 9 Just to... (9 Replies)
Discussion started by: shoaibjameel123
9 Replies

7. Shell Programming and Scripting

Adding values of a column based on another column

Hello, I have a data such as this: ENSGALG00000000189 329 G A 4 2 0 ENSGALG00000000189 518 T C 5 1 0 ENSGALG00000000189 1104 G A 5 1 0 ENSGALG00000000187 3687 G T 5 1 0 ENSGALG00000000187 4533 A T 4 2 0 ENSGALG00000000233 5811 T C 4 2 0 ENSGALG00000000233 5998 C A 5 1 0 I want to... (3 Replies)
Discussion started by: Homa
3 Replies

8. UNIX for Dummies Questions & Answers

Changing the values of a column using awk and gsub

Hi, I am using the following code to change NA to X in only the 5th column of my text file: awk '{gsub("NA","x",$5)}1' in.file > out.file How can I modify this code if I want to change NA to X in multiple columns of the text file (i.e. columns 5,6 and 7). Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

9. Shell Programming and Scripting

How to averaging column based on first column values

Hello I have file that consist of 2 columns of millions of entries timestamp and throughput I want to find the average (throughput ) for each equal timestamp before change it to proper format e.g : i want to average 2 coloumnd fot all 1308154800 values in column 1 and then print... (4 Replies)
Discussion started by: aadel
4 Replies

10. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
Login or Register to Ask a Question