Compare 2 columns from the same file and print a value depending on the result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare 2 columns from the same file and print a value depending on the result
# 1  
Old 09-06-2017
Question Compare 2 columns from the same file and print a value depending on the result

Hello Unix gurus,

I have a file with this format (example values):
Code:
label1	1	0
label2	1	0
label3	0.4	0.6
label4	0.5	0.5
label5	0.1	0.9
label6	0.9	0.1

in which:
column 1 is a row label
column 2 and 3 are values

I would like to do a simple operation on this table and get the following output file:
Code:
label1	0
label2	0
label3	2
label4	1
label5	2
label6	0

in which the value in column 2 is:
"0" if input column 2 > input column 3
"1" if input column 2 = input column 3
"2" if input column 2 < input column 3

I think is possible in bash/awk but I am not sure (other possibilities welcome)

Many thanks for your help and time.

KS

Last edited by ksennin; 09-06-2017 at 04:01 PM..
# 2  
Old 09-06-2017
Any attempts / ideas / thoughts from your side? Even partial attempts welcome.
# 3  
Old 09-06-2017
Quote:
Originally Posted by RudiC
Any attempts / ideas / thoughts from your side? Even partial attempts welcome.
Thanks for your quick reply!
Of course. So far I have:

Code:
awk '{
if ($3 > $2)
	print $1,"2";
else
	print $1,"0";
}' file1 > file2

but I am not sure how to deal with the case in which $3 and $2 would be the exact same value.

thanks
# 4  
Old 09-06-2017
Mayhap another if ... then ... construct?
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-06-2017
Sorry for the simplicity, I'm just starting! Smilie
I didn't know how to combine them with "else if".

Code:
awk '{
if ($3 > $2)
	print $1,"2";
else if($3 < $2)
	print $1,"0";
else if ($3 = $2)
	print $1,"1";
}' file1

# 6  
Old 09-06-2017
So you've done it on your own! Congrats!

Next time you might consider
Code:
awk '{print $1, ($2>$3)?0:($2<$3)?1:2}' file3
label1 0
label2 0
label3 1
label4 2
label5 1
label6 0

as well.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-06-2017
Thank you for the kind word! It's a very slow process Smilie

Quote:
Originally Posted by RudiC
So you've done it on your own! Congrats!

Next time you might consider
Code:
awk '{print $1, ($2>$3)?0:($2<$3)?1:2}' file3
label1 0
label2 0
label3 1
label4 2
label5 1
label6 0

as well.
Do you just list the conditions as x:y:z with the last one being a bit like "else"?
Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split a non delimited file into columns depending on user input

I would like some advice on some code. I want to write a small script that will take an input file of this format 111222233334444555666661112222AAAA 2222333445556612323244455445454545 2334556345643534505435345353453453 (and so on) It will be called as : script inputfile X (where X is... (5 Replies)
Discussion started by: onlyforbopi
5 Replies

2. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

3. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

4. Shell Programming and Scripting

awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3. FILE1 1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04 1467 55.27433... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

5. Shell Programming and Scripting

awk: print columns depending on their number

hi guys, i have the following problem: i have a matrix with 3 columns and over 450 rows like this: 0.0165 0.0151 0.0230 0.0143 0.0153 0.0134 0.0135 0.0123 0.0195 0.0173 0.0153 0.0182 i now want to calculate the average of every line and divide every element of this... (1 Reply)
Discussion started by: waddle
1 Replies

6. Shell Programming and Scripting

Compare columns 2 files and print

File 1 has 16 columns so does File 2 I want to remove all records from File 2 that column 1 and column 16 match between file 1 and file 2 delimter of files is ~ (10 Replies)
Discussion started by: sigh2010
10 Replies

7. Shell Programming and Scripting

compare two columns of different files and print the matching second file..

Hi, I have two tab separated files; file1: S.No ddi fi cu o/l t+ t- 1 0.5 0.6 o 0.1 0.2 2 0.2 0.3 l 0.3 0.4 3 0.5 0.8 l 0.1 0.6 ... (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

8. Shell Programming and Scripting

Compare selected columns from a file and print difference

I have learned file comparison from my previous post here. Then, it is comparing the whole line. Now, i have a new problem. I have two files with 3 columns separated with a "|". What i want to do is to compare the second and third column of file 1, and the second and third column of file 2. And... (4 Replies)
Discussion started by: kingpeejay
4 Replies

9. UNIX for Dummies Questions & Answers

find common lines using just one column to compare and result with all columns

Hi. If we have this file A B C 7 8 9 1 2 10 and this other file A C D F 7 9 2 3 9 2 3 4 The result i´m looking for is intersection with A B C D F so the answer here will be (10 Replies)
Discussion started by: alcalina
10 Replies

10. Shell Programming and Scripting

compare columns from seven files and print the output

Hi guys, I need some help to come out with a solution . I have seven such files but I am showing only three for convenience. filea a5 20 a8 16 fileb a3 42 a7 14 filec a5 23 a3 07 The output file shoud contain the data in table form showing first field of... (7 Replies)
Discussion started by: smriti_shridhar
7 Replies
Login or Register to Ask a Question