如何在linux中使用join命令将两个不同长度、不同列的文本文件与标题匹配

如何在linux中使用join命令将两个不同长度、不同列的文本文件与标题匹配,linux,shell,join,Linux,Shell,Join,我有两个不同长度的文本文件A.txt和B.txt A.txt看起来像: ID pos val1 val2 val3 1 2 0.8 0.5 0.6 2 4 0.9 0.6 0.8 3 6 1.0 1.2 1.3 4 8 2.5 2.2 3.4 5 10 3.2 3.4 3.8 pos category 2 A 4 B 6 A 8 C 10 B B.t

我有两个不同长度的文本文件A.txt和B.txt

A.txt看起来像:

ID  pos  val1  val2  val3
1   2    0.8     0.5   0.6
2   4    0.9     0.6   0.8
3   6    1.0     1.2   1.3
4   8    2.5     2.2   3.4
5   10   3.2     3.4   3.8
pos category
2    A
4    B
6    A
8    C
10   B
B.txt看起来像:

ID  pos  val1  val2  val3
1   2    0.8     0.5   0.6
2   4    0.9     0.6   0.8
3   6    1.0     1.2   1.3
4   8    2.5     2.2   3.4
5   10   3.2     3.4   3.8
pos category
2    A
4    B
6    A
8    C
10   B
我想在这两个文件中匹配pos列和,并希望输出如下

ID  catgeory  pos  val1  val2  val3
1      A       2    0.8     0.5   0.6
2      B       4    0.9     0.6   0.8
3      A       6    1.0     1.2   1.3
4      C       8    2.5     2.2   3.4
5      B       10   3.2     3.4   3.8
我使用了join函数join-12-21,如果您对awk没有问题,请尝试以下内容。使用GNU awk中显示的样本编写和测试

说明:增加对以上内容的详细说明

awk '                       ##Starting awk program from here.
FNR==NR{                    ##Checking condition FNR==NR which will be TRUE when B.txt is being read.
  a[$1]=$2                  ##Creating array a with index of 1st field and value is 2nd field of current line.
  next                      ##next will skip all further statements from here.
}
($2 in a){                  ##Checking condition if 2nd field is present in array a then do following.
  $2=a[$2] OFS $2           ##Adding array a value along with 2nd field in 2nd field as per output.
}
1                           ##1 will print current line.
' B.txt A.txt | column -t   ##Mentioning Input_file names and passing awk program output to column to make it look better.
如果您对awk还满意,请尝试以下内容。使用GNU awk中显示的样本编写和测试

说明:增加对以上内容的详细说明

awk '                       ##Starting awk program from here.
FNR==NR{                    ##Checking condition FNR==NR which will be TRUE when B.txt is being read.
  a[$1]=$2                  ##Creating array a with index of 1st field and value is 2nd field of current line.
  next                      ##next will skip all further statements from here.
}
($2 in a){                  ##Checking condition if 2nd field is present in array a then do following.
  $2=a[$2] OFS $2           ##Adding array a value along with 2nd field in 2nd field as per output.
}
1                           ##1 will print current line.
' B.txt A.txt | column -t   ##Mentioning Input_file names and passing awk program output to column to make it look better.

按照你的要求。。。仅使用GNU连接即可获得所需的输出:


按照你的要求。。。仅使用GNU连接即可获得所需的输出:


谢谢,@ravinderSingh13但我在加入时看起来还是一样function@Jammy,当我运行命令join-12-21谢谢,@ravinderSingh13,但我在join中看起来是一样的function@Jammy,当我运行命令join-12-21时,当我运行你的join命令时,我在C.txt的最后一行得到完全不同的列顺序和标题。当我运行你的join命令时,我得到了完全不同的列顺序和C.txt最后一行的标题。
$ join -1 2 -2 1 <(sort -k2 -g A.txt)  <(sort -k1 -g B.txt) -o 1.1,2.2,1.2,1.3,1.4,1.5 | column -t
ID  category  pos  val1  val2  val3
1   A         2    0.8   0.5   0.6
2   B         4    0.9   0.6   0.8
3   A         6    1.0   1.2   1.3
4   C         8    2.5   2.2   3.4
5   B         10   3.2   3.4   3.8
$