如何比较和排序2 CSV显示差异 [英] How to compare and sort 2 csv's to show difference
问题描述
我有2个csv,格式如下(基本上是电子邮件列表和该发件人发送电子邮件的次数):
Hi I have 2 csv's in the following format, (basically a list of email and the number of times we have been emailed by that sender):
file1.csv
file1.csv
Email,Value
email1@email.com,2
email2@email.com,4
email3@email.com,1
email4@email.com,6
file2.csv
file2.csv
Email,Value
email1@email.com,3
email2@email.com,6
email3@email.com,8
email4@email.com,2
每个表中的值可能不同,我想做的就是将结果输出到新的CSV中,如下所示:
the values in each table might be different and what I want to do is output the results to a new CSV which looks like this:
file3.csv
file3.csv
Email,Value1,Value2
email1@email.com,2,3
email2@email.com,4,6
email3@email.com,1,8
email4@email.com,6,2
我有一个bash
脚本,该脚本是我从其他地方获取的,它确实完成了一部分工作,但未列出我要查找的结果.
I've got a bash
script that I took from elsewhere and it does part of the job but It doesnt list the results I am looking for.
有人可以为此提供bash脚本吗?
Can anyone help with a bash script for this?
#!/bin/bash
join -t"," -1 1 -2 1 -a1 file1.csv file2.csv | awk -F, ' BEGIN {
print "Email,Value"
} NF > 3 {
if ( $3 != $5 )
print $1, $3, $5
if ( $2 != $4 )
print $1, $2, $4
} ' OFS=,
这是我通过使用以下命令得到的结果:
This is the result I am getting by using the following:
awk 'BEGIN{FS=OFS=","; printf "Name,Value1,Value2\n"}NR >1 &&
FNR==NR{map[$1]=$2; next}$1 in map{$(NF+1)=map[$1]; print}' file2.csv
file1.csv
结果:
Name,Value1,Value2
,3ail1@email.com,2
,6ail2@email.com,4
,8ail3@email.com,1
Email4@email.com,6,2
推荐答案
如果您想保留订单
awk
进行救援!
$ awk 'BEGIN {FS=OFS=","}
NR==FNR {a[$1]=$2; next}
FNR==1 {print $1,$2"1",a[$1]"2"; next}
{print $1,$2,a[$1]}' file2 file1
Email,Value1,Value2
email1@email.com,2,3
email2@email.com,4,6
email3@email.com,1,8
email4@email.com,6,2
注意文件的顺序...
note the order of files...
这篇关于如何比较和排序2 CSV显示差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!