根据匹配的列数据连接两个 CSV 文件

根据匹配的列数据连接两个 CSV 文件

我想根据匹配的列信息连接两个 CSV 文件中的数据。要匹配的数据来自 File1.csv 第 5 列和 File2 第 1 列,我想在匹配时附加 File2 第 2 列中的信息,如果不匹配,则保留空双引号。

文件1.csv

"Z","P","W","K","1","1.18.24.59"
"S","K","D","X","9","1.14.19.238"
"R","M","P","Y","8","1.15.11.21"
"B","D","0","U","5","1.9.20.159"
"R","E","W","Q","6","135.0.0.1"
"K","D","K","R","9","1.9.74.13"

文件2.csv

"65.9.7.19","374 22 53"
"1.9.74.13","123 256 51"
"1.18.24.59","23 25 41"
"1.15.11.21","98 77 8291"
"1.14.19.238","8827 145 8291"
"1.9.20.159","283 1 5734"

所需输出

"Z","P","W","K","1","1.18.24.59","23 25 41"
"S","K","D","X","9","1.14.19.238","8827 145 8291"
"R","M","P","Y","8","1.15.11.21","98 77 8291"
"B","D","0","U","5","1.9.20.159","283 1 5734"
"R","E","W","Q","6","135.0.0.1",""
"K","D","K","R","9","1.9.74.13","123 256 51"

答案1

这是一种解决方案,使用awk.在 GNU awk 4.1.3 上测试。

$ awk -F, 'NR==FNR{a[$1]=$2}NR!=FNR{print $0","(a[$6]?a[$6]:"\"\"")}' file2.csv file1.csv
"Z","P","W","K","1","1.18.24.59","23 25 41"
"S","K","D","X","9","1.14.19.238","8827 145 8291"
"R","M","P","Y","8","1.15.11.21","98 77 8291"
"B","D","0","U","5","1.9.20.159","283 1 5734"
"R","E","W","Q","6","135.0.0.1",""
"K","D","K","R","9","1.9.74.13","123 256 51"
$
  • 匹配NR==FNR{a[$1]=$2file2 中的行,并将它们添加到一个数组中,键入字段 #1。
  • 然后NR!=FNR匹配 file1 中的行。
  • print $0","打印 file1 中的整行,后跟一个逗号。
  • (a[$6]?a[$6]:"\"\"")打印之前构建的数组的相应内容,但前提""是没有找到条目。

答案2

csvkit事实证明这比我预期的要难,但是使用基于 python 的1.0.2 版本中的工具:

$ csvjoin -HI -c 6,1 --left File1.csv File2.csv 2>/dev/null | csvformat -K1 -U1
"Z","P","W","K","1","1.18.24.59","23 25 41"
"S","K","D","X","9","1.14.19.238","8827 145 8291"
"R","M","P","Y","8","1.15.11.21","98 77 8291"
"B","D","0","U","5","1.9.20.159","283 1 5734"
"R","E","W","Q","6","135.0.0.1",""
"K","D","K","R","9","1.9.74.13","123 256 51"

(这-I似乎有必要防止csvjoin猜测点分隔字段是日期时间,至少在我的语言环境中是这样。)

答案3

for i in `cat file2.csv | awk -F "," '{print $1}'| sed 's/"//g'`; do j=`sed -n '/'$i'/p' file2.csv| awk -F "," '{print $2}'| sed 's/"//g'`;awk -v i="$i" -v j="$j" -F "," '$NF==i{$7=j;print $0}' file1.csv; done| sed 's/ /","/g'| sed 's/^/"/g'| sed 's/$/"/g'

输出

"Z","P","W","K","1","1.18.24.59","23 25 41"
"S","K","D","X","9","1.14.19.238","8827 145 8291"
"R","M","P","Y","8","1.15.11.21","98 77 8291"
"B","D","0","U","5","1.9.20.159","283 1 5734"
"R","E","W","Q","6","135.0.0.1",""
"K","D","K","R","9","1.9.74.13","123 256 51"

相关内容