gpt4 book ai didi

bash - 加入命令,省略一行数字

转载 作者:行者123 更新时间:2023-11-29 09:51:48 25 4
gpt4 key购买 nike

我有两个文件,我想取出第三列中有共同数据的行。但是它遗漏了应该匹配的行。

文件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 之后,它也遗漏了公共(public)字段为 3 的行

编辑:

预期输出:

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 - 加入命令,省略一行数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38389920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com