Home > Net >  How to compare two different files and two different columns bash
How to compare two different files and two different columns bash

Time:04-26

file 1

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

file 2

abs,124235235
dsg,262356527

If second columnt from first file = first column from the second file, then add second column from second file in the first column 1 file.

I need to get:

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

How can I do this?

That's my attempts, but i don't understand very much

#!/bin/bash

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

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

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

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


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

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

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

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

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

CodePudding user response:

This should do the trick:

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

CodePudding user response:

Just using bash's associative and indexed arrays:

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 <"$2"

    # 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 <"$1"
}

f "file 1" "file 2"
  • Related