文件A有以下3列: AccountNumber,客户名称,余额
文件B有以下5列 SystemID,AccountNumber,Product,OpeningDate
我使用下面的AWK代码加入这两个文件。它无法正常工作
FNR==NR{a[$1]=$0;next}$2 in a{ print $0, a[$1]}
两个文件的组合输出预计如下 SystemID,AccountNumber,Product,OpeningDate,CustomerName,Balance
仅在AWK代码中请求帮助。我知道通过SORT和JOIN可以获得类似的功能,但我只是在寻找AWK解决方案。
File A: +---------------+---------------+----------+ | AccountNumber | CustomerName | Balance | +---------------+---------------+----------+ | 3 | C | 100 | | 4 | A | 200 | | 5 | B | 300 | +---------------+---------------+----------+ File B: +-----------+----------------+----------+--------------+ | SystemID | AccountNumber | Product | OpeningDate | +-----------+----------------+----------+--------------+ | 1 | 6 | RD | 12-05-17 | | 2 | 4 | SB | 15-05-17 | | 3 | 3 | TD | 02-04-17 | | 4 | 5 | SB | 15-01-17 | +-----------+----------------+----------+--------------+ Output after joining: +-----------+----------------+----------+--------------+--------------+---------+ | SystemID | AccountNumber | Product | OpeningDate | CustomerName | Balance | +-----------+----------------+----------+--------------+--------------+---------+ | 2 | 4 | SB | 15-05-17 | A | 200 | | 3 | 3 | TD | 02-04-17 | C | 100 | | 4 | 5 | SB | 15-01-17 | B | 300 | +-----------+----------------+----------+--------------+--------------+---------+
答案 0 :(得分:1)
如果你可以删除管道,加上和 - 下面的符号是解决方案 -
cat file1
1 6 RD 12-05-17
2 4 SB 15-05-17
3 3 TD 02-04-17
4 5 SB 15-01-17
cat file2
3 C 100
4 A 200
5 B 300
awk 'NR==FNR{a[$1]=$2FS$3;next} $2 in a {print $0, a[$2]}' file2 file1
2 4 SB 15-05-17 A 200
3 3 TD 02-04-17 C 100
4 5 SB 15-01-17 B 300