匹配2个文件中的2列,并从第一个文件中获取另一个值

问题描述 投票:1回答:2

我有2个csv文件,它们具有以下结构:

File 1:
date,keyword,location,page
2019-04-11,ABC,mumbai,http://www.insurers.com
and so on.

File 2:
date,site,market,location,url 
2019-05-12,denmark,de ,Frankfurt,http://lufthansa.com
2019-04-11,Netherlands,nl,amsterdam,http://www.insurers.com

问题是我需要匹配文件中的日期以及网址。例:

2019-04-11 and http://www.insurers.com (File 1)
with 
2019-04-11 and http://www.insurers.com (File 2)

编辑:如果满足此条件,则应将文件1中的关键字(ABC)作为第三列(新列)插入文件2中。

预期产出:

date,site,keyword,market,location,url
2019-04-11,Netherlands,ABC,nl,amsterdam,http://www.insurers.com

我已经尝试将日期和网址放在java中的地图中,但有太多的网址重复。所以我正在寻求一个bash,awk,grep或sed解决方案。谢谢。

awk sed grep text-processing
2个回答
2
投票
$ awk '
    BEGIN { FS=OFS="," }
    NR==FNR { m[$1,(NR>1?$4:"url")]=$2; next }
    ($1,$5) in m { $2=$2 OFS m[$1,$5]; print }
' file1 file2
date,site,keyword,market,location,url
2019-04-11,Netherlands,ABC,nl,amsterdam,http://www.insurers.com

0
投票

但尝试GNU;

sed -En 's!^([0-9]{4}-[0-9]+-[0-9]+,).+(http://\w.+)!s#^\1([^,]+),[^,]+,\\s*\2#\\1#p!p' File2| sed -Enf - File1 >Result
© www.soinside.com 2019 - 2024. All rights reserved.