How to compare and sort 2 csv's to show difference

后端 未结 4 2011
我在风中等你
我在风中等你 2021-01-24 18:28

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

Email,Val         


        
4条回答
  •  太阳男子
    2021-01-24 19:02

    Pretty straight-forward with Awk!

    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 file1
    

    produces

    Name,Value1,Value2
    email1@email.com,2,3
    email2@email.com,4,6
    email3@email.com,1,8
    email4@email.com,6,2
    

    Set input and output field-separator to , in the BEGIN clause that gets executed before the input lines are processed and also the final header information needed. The part FNR==NR is run for the first file in order file2 in this case, create a hash-map, with an index set to the $1 and value set to $2 then on file1 for those lines whose hashed index value belongs in $1 create a new field $(NF+1) meaning the last field + 1 to the new value and print the result formed.

提交回复
热议问题