我有两个文件,我想取出第三列中有共同数据的行。但它遗漏了一行应该匹配的行。
文件1
b b b
4 5 3
c c c
文件2
1 2 3 4
a b c d
e f g h
i j k l
l m n o
输出为:
c c c a b d
使用的命令是:
join -1 3 -2 3 --nocheck-order File1.txt File2.txt
即使在放置了 --nocheck-order 之后,它仍然遗漏了以 3 作为公共(public)字段的行
编辑:
预期输出:
c c c a b d
3 4 5 1 2 4
最佳答案
作为 2 个 sort
命令(对于大文件可能非常昂贵)和一个 join
的替代方案,您可以使用这个单个 awk
命令来获取输出:
awk 'FNR == NR{a[$3]=$0; next} $3 in a{print $3, a[$3], $1, $2, $4}' file1 file2
3 4 5 3 1 2 4
c c c c a b d
说明:
NR == FNR { # While processing the first file
a[$3] = $0 # store the whole line in array a using $3 as key
next
}
$3 in a { # while processing the 2nd file, when $3 is found in array
print $3,a[$3],$1,$2,$4 # print relevant fields from file2 and the remembered
# value from the first file.
}
关于bash - join 命令遗漏了一行数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38389920/