首页 > 解决方案 > awk 比较同一文件的两列并在新列中打印输出

问题描述

我正在尝试比较 txt 文件中两列的值,以确定第一列的值是否比第二列的值大,并且我希望第三列说“这里是主要的”或“这里少了”。

这里有一个例子:

col 1                      col 2
0.19727856043434921   0.3970115896012234                   
0.23832706720231472   0.25380634765625        
0.12105356101624322   0.3748512347530257        
0.28048626091230955   0.05645118584277009 

  

我想要类似的东西

col 1                  col 2 
0.19727856043434921   0.3970115896012234        here is less            
0.23832706720231472   0.25380634765625          here is less
0.12105356101624322   0.3748512347530257        here is less
0.28048626091230955   0.05645118584277009       here is major

我尝试如下使用 awk 但没有检测到真相

awk 'OFS="\t" {if( $1>$2) print "here is major"; else if( $1<$2) print "here is less"} ' file.txt > file2.txt

标签: fileawkmatching

解决方案


推荐阅读