如何比较两个不同的文件和两个不同的列 bash

How to compare two different files and two different columns bash

文件 1

Client ID,USER ID,DH SERV, ...
,abs,2022-04-24, ...
,btg,2022-04-24, ...

文件 2

abs,124235235
dsg,262356527

如果第一个文件的第二列 = 第二个文件的第一列,则将第二个文件的第二列添加到第一列 1 文件中。

我需要得到:

Client ID,USER ID,DH SERV, ...
124235235,abs,2022-04-24, ...
,btg,2022-04-24, ...

我该怎么做?

这是我的尝试,但我不是很明白

#!/bin/bash

#awk -F, 'FNR==NR{a[]=[=14=];next} ( in a){print ,a[]}' mgcom_Deloviye_Linii_RU_conve_2022-04-24.csv wamfactory_6100.csv > test

#awk -F, 'NR==FNR{a[FNR]=; next} { == a[FNR] ? a[FNR]","[=14=] : [=14=]}' mgcom_Deloviye_Linii_RU_conve_2022-04-24.csv wamfactory_6100.csv > test

#awk -F, 'NR==FNR{a[FNR]=; next} { == a[FNR] ? a[FNR]","[=14=] : [=14=]}' wamfactory_6100.csv mgcom_Deloviye_Linii_RU_conve_2022-04-24.csv > test

#awk -F, '{print FILENAME, NR, FNR, a[FNR]=,"||", b[NR]=}' mgcom_Deloviye_Linii_RU_conve_2022-04-24.csv wamfactory_6100.csv > test


#Work
#awk -F, 'NR==FNR{A[]; next} in A' mgcom_Deloviye_Linii_RU_conve_2022-04-24.csv wamfactory_6100.csv > test

#awk -F, 'NR==FNR{A[NR]=; next}( in A) {print A[NR]}' wamfactory_6100.csv mgcom_Deloviye_Linii_RU_conve_2022-04-24.csv> test

#awk -F, 'NR==FNR{A[]=; next} in A{print A[]}' mgcom_Deloviye_Linii_RU_conve_2022-04-24.csv wamfactory_6100.csv  > test

#awk -F, 'FNR==NR{A[]=; next} in A{print A[]}' mgcom_Deloviye_Linii_RU_conve_2022-04-24.csv wamfactory_6100.csv  > test

awk -F, 'NR==FNR {arr[]= ; next}
        {print arr[]","[=14=]}
        ' wamfactory_6100.csv mgcom_Deloviye_Linii_RU_conve_2022-04-24.csv > test

这应该可以解决问题:

awk 'BEGIN{FS=OFS=","}NR==FNR{a[]=} NR!=FNR{if( in a){print a[],,, }else{print [=10=]}}'  file2 file1
Client ID,USER ID,DH SERV, ...
124235235,abs,2022-04-24, ...
,btg,2022-04-24, ...

仅使用 bash 的关联数组和索引数组:

f(){
    # delimit input/output with comma not whitespace
    local IFS=,

    # treat m as an associative array
    declare -A m

    # initialize m from second file
    while read -r k v; do
        m[$k]=$v
    done <""

    # process lines of the first file
    while read -ra i; do
        # get second column
        k=${i[1]}

        # attempt substitution
        i[1]=${m[$k]:-$k}

        # print the line (quoting forces interpolation of IFS)
        echo "${i[*]}"
    done <""
}

f "file 1" "file 2"
$ awk 'BEGIN{FS=OFS=","} NR==FNR{a[]=; next}  in a{=a[]} 1' file2 file1
Client ID,USER ID,DH SERV, ...
124235235,abs,2022-04-24, ...
,btg,2022-04-24, ...