bash - join 命令遗漏了一行数字

标签 bash shell join

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

文件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/

相关文章:

linux - 从文件中查找定义的数据并用作变量

Bash - 如何将参数传递给通过标准输入读取的脚本

bash - IBM/secure-gateway-client docker与--F(acl文件)选项一起运行

linux - 在 bash 脚本中查找并替换字符串(从字符串列表中)

MySQL - 根据从表 X 中删除的结果从表 Y 中删除

mysql - 如何将 CSV 列表连接到表列

git - 如何在 oh-my-zsh 中使用 posh-git-bash?

bash - 打印与用户名关联的名称和主目录路径

linux - 带有 put *.* 的 ftp 命令不发送所有文件

sql - 自定义表连接