我是linux命令的新手。如果一个文件的值(field2(小于另一个文件else return 0
中的值(field 2(,请帮助我执行命令/脚本并解释return 1
输入:
file1.text
AA 90
BB 80.60
CC 60.50
file2.txt
AA 98.60
BB 88.76
CC 100.90
输出:file3.txt
AA 1
BB 1
CC 1
使用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