在 bash (Linux) 中从另一个 csv 中查找值(如 vlookup) [英] Find value from one csv in another one (like vlookup) in bash (Linux)

查看:14
本文介绍了在 bash (Linux) 中从另一个 csv 中查找值(如 vlookup)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了所有在网上找到的方法来解决我的问题,但效果不佳.

I have already tried all options that I found online to solve my issue but without good result.

基本上我有两个 csv 文件(管道分隔):

Basically I have two csv files (pipe separated):

file1.csv:

123|21|0452|IE|IE|1|MAYOBAN|BRIN|OFFICE|STREET|MAIN STREET|MAYOBAN|

123|21|0453|IE|IE|1|CORKKIN|ROBERT|SURNAME|CORK|APTS|CORKKIN|

123|21|0452|IE|IE|1|CORKCOR|NAME|HARRINGTON|DUBLIN|STREET|CORKCOR|

file2.csv:

MAYOBAN|BANGOR|2400

MAYOBEL|BELLAVARY|2400

CORKKIN|KINSALE|2200

CORKCOR|CORK|2200

DUBLD11|DUBLIN 11|2100

我需要一个 linux bash 脚本来根据 file1 中 pos7 的内容从 file2 中找到 pos.3 的值.

I need a linux bash script to find the value of pos.3 from file2 based on the content of pos7 in file1.

例子:

file1, line1, pos 7: MAYOBAN
find MAYOBAN in file2, return pos 3 (2400)

输出应该是这样的:

**2400**

**2200**

**2200**

**etc...**

请帮忙杰克

推荐答案

一点方法,远未完美:

DELIMITER="|"

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

这篇关于在 bash (Linux) 中从另一个 csv 中查找值(如 vlookup)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆