如何在一个相应的列上连接两个单独的文件?

问题描述 投票:0回答:3

我正在尝试在一列上连接两个文件,但join和sort命令为我提供了以下输出:

join: file 1 is not in sorted order

档案1:

TEST->Infrastructure->Global Windows Server, OI-QASDWDASDWQWD,
TEST->Infrastructure->Global Windows Server, OI-WASDWDASDWWWW,
TEST->Infrastructure->zSeries_MVS, REGAA638G0K,
TEST->Infrastructure->zSeries_MVS, REGAA55410K,

文件2:

SERVER1; Deployed; REGAA638G0K;
SERVER2; Deployed; OI-WASDWDASDWWWW;
SERVER3; Delete; OI-QASDWDASDWQWD;
SERVER4; Delete; REGAA55410K;

预期文件3:

SERVER1; Deployed; TEST->Infrastructure->zSeries_MVS;
SERVER2; Deployed; TEST->Infrastructure->Global Windows Server;
SERVER3; Delete; TEST->Infrastructure->Global Windows Server;
SERVER4; Delete; TEST->Infrastructure->zSeries_MVS;

我的命令:

join -1 2 -2 3 -o 1.1,2.1,2.2 <(sort -t"," -k2 spmGroupsModifiedSCLine.out) <(sort -t";" -k3 spmCompStatJoined.out)

第一个文件中的第二列和第二个文件中的第三列是相同的,所以我试图加入它并首先对其进行排序。你看到其他方式加入吗?谢谢 !

xml linux bash sorting join
3个回答
0
投票

Awk解决方案:

awk 'NR==FNR{ a[$2]=$1; next }$3 in a{ print $1,$2,a[$3] }' FS=',' file1 FS=';' OFS='; ' file2

输出:

SERVER1;  Deployed; TEST->Infrastructure->zSeries_MVS
SERVER2;  Deployed; TEST->Infrastructure->Global Windows Server
SERVER3;  Delete; TEST->Infrastructure->Global Windows Server
SERVER4;  Delete; TEST->Infrastructure->zSeries_MVS

0
投票

不像awk解决方案那么优雅,但可能更直观:

cat file2 | while read line; do
  key=$(cut -d';' -f3 <<< $line)
  echo "$(cut -d';' -f1-2 <<< $line); $(grep $key file1 | cut -d',' -f1);" >> file3
done

要么

cat file2 | while read line; do key=$(cut -d';' -f3 <<< $line); echo "$(cut -d';' -f1-2 <<< $line); $(grep $key file1 | cut -d',' -f1);"; done > file3

0
投票

如果你想使用join。

join -t ';' -1 2 -2 3 -o 2.1,2.2,1.1 <(sort -t , -k 2 File\ 1 | tr ',' ';') <(sort -t ';' -k 3 File\ 2) | sort > File\ 3
© www.soinside.com 2019 - 2024. All rights reserved.