在 bash 中连接两个以逗号分隔的文本文件

在 bash 中连接两个以逗号分隔的文本文件

文件1:

Doe, John, phone123
Dane, Jane, phone124

文件2

phone123, longdistance, 10mins
phone124, local, 5mins
phone124, longdistance, 6mins

输出

Doe, John phone123 longdistance 10mins
Dane, Jane phone124 local 5mins
Dane, Jane phone124 longdistance 6mins

到目前为止,我已经

join -1 1 -2 3 -o 2.1,2.2,1.1,1.2,1.3 file2.txt file1.txt > output.txt

如果文件 1 和 2 仅由空格分隔而没有“,”,则它可以工作,但是当我将逗号放入文件中时,它会输出空白。

答案1

真正的问题是 2 字符字段分隔符。像 join 和 sort 这样的工具期望分隔符只是一个字符:

join -t, -13 -21 -o 1.1,1.2,1.3,2.2,2.3 \
  <(sed -E 's/, +/,/g' file1.txt | sort -t, -k3,3) \
  <(sed -E 's/, +/,/g' file2.txt | sort -t, -k1,1)

输出

Doe,John,phone123,longdistance,10mins
Dane,Jane,phone124,local,5mins
Dane,Jane,phone124,longdistance,6mins

您可能希望将其通过管道传输| sed 's/,/, /g'以恢复“逗号空格”分隔符。


交替,

awk '
    BEGIN {FS = ", *"; OFS = ", "}
    NR == FNR {name[$3] = $1 OFS $2; next}
    $1 in name {print name[$1], $0}
' file1.txt file2.txt

相关内容