bash Join命令,省略一行数字 [英] bash Join command, Leaving out a row of numbers

查看:107
本文介绍了bash Join命令,省略一行数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two files, I want to take out the rows which have common data in the third column. But it is leaving out a row which should be matched.

File1

b b b
4 5 3
c c c

File2

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

It is missing out the row with 3 as the common field, even after placing the --nocheck-order

预期输出:

c c c a b d
3 4 5 1 2 4

推荐答案

作为2个sort命令(对于大文件可能非常昂贵)和join的替代,您可以使用单个awk命令获取您的输出:

As an alternative to 2 sort commands (can be very expensive for big files) and then a join, you can use this single awk command to get your output:

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命令,省略一行数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆