Find value from one csv in another one (like vlookup) in bash (Linux)

∥☆過路亽.° 提交于 2019-11-30 16:38:15

A little approach, far away to be perfect:

DELIMITER="|"

for i in $(cut -f 7 -d "${DELIMITER}" file1.csv ); 
do 
    grep "${i}" file2.csv | cut -f 3 -d "${DELIMITER}"; 
done

This will work, but since the input files must be sorted, the output order will be affected:

join -t '|' -1 7 -2 1 -o 2.3 <(sort -t '|' -k7,7 file1.csv) <(sort -t '|' -k1,1 file2.csv)

The output would look like:

2200
2200
2400

which is useless. In order to have a useful output, include the key value:

join -t '|' -1 7 -2 1 -o 0,2.3 <(sort -t '|' -k7,7 file1.csv) <(sort -t '|' -k1,1 file2.csv)

The output then looks like this:

CORKCOR|2200
CORKKIN|2200
MAYOBAN|2400

Edit:

Here's an AWK version:

awk -F '|' 'FNR == NR {keys[$7]; next} {if ($1 in keys) print $3}' file1.csv file2.csv

This loops through file1.csv and creates array entries for each value of field 7. Simply referring to an array element creates it (with a null value). FNR is the record number in the current file and NR is the record number across all files. When they're equal, the first file is being processed. The next instruction reads the next record, creating a loop. When FNR == NR is no longer true, the subsequent file(s) are processed.

So file2.csv is now processed and if it has a field 1 that exists in the array, then its field 3 is printed.

cut -d\| -f7 file1.csv|while read line
do 
  grep $line file1.csv|cut -d\| -f3
done
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!