比较文件中的值

时间:2018-10-30 06:00:24

标签: linux unix compare

是Linux命令的新手。如果一个文件的value(field2)小于另一个文件employee中的value(field2),请向我提供命令/脚本,向return 1进行解释

输入:

file1.txt

else return 0

file2.txt

AA 90  
BB 80.60  
CC 60.50

输出: file3.txt

AA 98.60  
BB 88.76  
CC 100.90

1 个答案:

答案 0 :(得分:0)

使用awk:

$ awk 'NR==FNR{a[$1]=$2;next}($1 in a){print $1, a[$1]<$2}' file1 file2

输出:

AA 1
BB 1
CC 1

解释了一些:

$ awk '                 # using awk
NR==FNR {               # process first file
    a[$1]=$2            # hash to a, $1 is the key
    next                # process the next record
}                       # second file processing follows
($1 in a) {             # if $1 was present in file1
    print $1, a[$1]<$2  # print $1 and 0/1 whether file1 entry was less or not
}' file1 file2