Operating System - HP-UX
1751877 Members
5263 Online
108782 Solutions
New Discussion юеВ

Re: Comparing columns in a file help

 
SOLVED
Go to solution
Michael Barron
Occasional Advisor

Comparing columns in a file help

I have a file that contains 2 columns.

file1:
10.994 14.514
13.506 16.587
16.156 15.597
14.008 13.609
12.617 14.532

I need to compare the 2 columns and determine that if column 2's value is greater than column 1's value by double or more I want to know about it. basically just print the row that meets that above standards. Any ideas?


6 REPLIES 6
Michael Mike Reaser
Valued Contributor

Re: Comparing columns in a file help

None of the data you show meets your criteria of "column 2's value is greater than column 1's value by double or more".

Or, are you wanting all lines where column 2 is simply greater than column 1?

Your data, and your description, do not go together, at all.

Now, presuming that your description is what you want, the following awk command should work for you:

awk '{a=$1*2;if(a>=$2){print $0}}' < input_file

This will display back to stdout all lines where the value in column 2 is twice or more the value in column 1.
There's no place like 127.0.0.1

HP-Server-Literate since 1979
Michael Barron
Occasional Advisor

Re: Comparing columns in a file help

Mike,

Thanks, the file was just an example, there are hundreds and hundreds of lines in the file and happen to grab some that dont work. Thanks again for the idea. I'll test it out and see if its what I need, i appreciate your help
Michael Barron
Occasional Advisor

Re: Comparing columns in a file help

Th awk statement works, but I get mixed results.
1.242 4.363 (Works here)
1.268 8.955 (Works here)
1.346 0.888 (Not here)
1.195 0.992 (Not here)

Thanks again for your help


Michael Mike Reaser
Valued Contributor
Solution

Re: Comparing columns in a file help

That's because the conditional I listed is backwards. The correct awk command should be

awk '{a=$1*2;if(a<=$2){print $0}}' < input_file

Whoops, and sorry. :-)
There's no place like 127.0.0.1

HP-Server-Literate since 1979
Michael Barron
Occasional Advisor

Re: Comparing columns in a file help

Awesome, works perfect, thanks again!
Viktor Balogh
Honored Contributor

Re: Comparing columns in a file help

here is another solution with a while loop in ksh:

# while read col1 col2; do if [ $(echo $col1*2000 | bc ) -lt $(echo $col2*1000 | bc ) ];then echo $col1 $col2;fi;done < inputfile
****
Unix operates with beer.