Home > Back-end >  Comparison of two CSV files
Comparison of two CSV files

Time:11-26

I'm trying to use Awk to compare the content of two large CSV files (more than 5000 rows each) but I can't get what I want.

Here is my problem:

I have a first file (File1) with a list of names ($1) and cities ($2) whose structure looks like this:

john;london
marc;paris
karen;new york
ben;london
vic;dublin

I have a second file (File2) with other information where we find some names ($3) of File1:

45456;windows;john;454646
47764;mac;zack;470093
41225;mac;ben;622101
12634;windows;ben;218996
7856;windows;karen;637294
12;mac;finn;878317
2315;windows;beverly;221167
445;windows;lilly;12316
3232;mac;john;601316
4546;mac;fish;305035
487;windows;vic;447421
46464;mac;karen;468154

I would like to extract from File2 all the lines whose names appear in File1 while adding the cities associated to each name in File1. Here is an example of the result I am looking for:

45456;windows;john;454646;london
3232;mac;john;601316;london
7856;windows;karen;637294;new york
46464;mac;karen;468154;new york
41225;mac;ben;622101;london
12634;windows;ben;218996;london
487;windows;vic;447421;dublin

Could you help me?

CodePudding user response:

Build an associative array of the first file, making the name the index and the city the value. For the second file check if the name features in the aray, if yes, print the line and attach the city.

awk -F';' 'NR==FNR{a[$1]=$2}$3 in a{print $0";"a[$3]}' File1 File2
45456;windows;john;454646;london
41225;mac;ben;622101;london
12634;windows;ben;218996;london
7856;windows;karen;637294;new york
3232;mac;john;601316;london
487;windows;vic;447421;dublin
46464;mac;karen;468154;new york

CodePudding user response:

With bash, GNU sort and GNU join:

join -t ';' -1 1 -2 3 <(sort File1) <(sort -t ';' -k 3 File2) -o 2.1,2.2,1.1,2.4,1.2

Output:

12634;windows;ben;218996;london
41225;mac;ben;622101;london
45456;windows;john;454646;london
3232;mac;john;601316;london
46464;mac;karen;468154;new york
7856;windows;karen;637294;new york
487;windows;vic;447421;dublin

CodePudding user response:

Using

csvjoin -d ';' -H -c 3,1 File2 File1 | csvformat -D ';' | tail -n  2
  • Related