匹配 2 个文件中的 2 列并从第一个文件中获取另一个值
Match 2 columns in 2 files and get another value from the first file
我有 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
问题是我需要匹配文件和 url 中的日期。示例:
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
我试过将日期和 url 放在 java 的地图中,但是重复的 URL 太多了。
所以我正在寻找 bash、awk、grep 或 sed 解决方案。
谢谢
$ awk '
BEGIN { FS=OFS="," }
NR==FNR { m[,(NR>1?:"url")]=; next }
(,) in m { = OFS m[,]; print }
' file1 file2
date,site,keyword,market,location,url
2019-04-11,Netherlands,ABC,nl,amsterdam,http://www.insurers.com
试试 gnu sed:
sed -En 's!^([0-9]{4}-[0-9]+-[0-9]+,).+(http://\w.+)!s#^([^,]+),[^,]+,\s*#\1#p!p' File2| sed -Enf - File1 >Result
我有 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
问题是我需要匹配文件和 url 中的日期。示例:
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
我试过将日期和 url 放在 java 的地图中,但是重复的 URL 太多了。 所以我正在寻找 bash、awk、grep 或 sed 解决方案。 谢谢
$ awk '
BEGIN { FS=OFS="," }
NR==FNR { m[,(NR>1?:"url")]=; next }
(,) in m { = OFS m[,]; print }
' file1 file2
date,site,keyword,market,location,url
2019-04-11,Netherlands,ABC,nl,amsterdam,http://www.insurers.com
试试 gnu sed:
sed -En 's!^([0-9]{4}-[0-9]+-[0-9]+,).+(http://\w.+)!s#^([^,]+),[^,]+,\s*#\1#p!p' File2| sed -Enf - File1 >Result