I am looking to compare two files, printing only records with a matching ID number and without duplicate records.
I have two files:
file1.txt
contains:
Simons 0987768798980
West 09809867678
Vickers 768774564650
Simons 76867790987
Peterson 24346576865
Simons 76867790987
Holister 87879655456
Peterson 87686765766
And, file2.txt
contains:
768774564650 Harry
76867790987 Steve
0987768798980 Mary
0987768798980 Mary
76856009097 Ali
87879655456 Rick
87686765766 Martin
The desired outcome is:
Harry Vickers 768774564650
Steve Simons 76867790987
Mary Simons 0987768798980
Rick Holister 87879655456
Martin Peterson 87686765766
This is what I have tried:
ARGV[1]==FILENAME{id2lastname[$2]=$1;id2id[$2]=$2}
ARGV[2]==FILENAME{id2firstname[$1]=$2}
$1 in id2id{print id2firstname[$1],id2lastname[$1],id2id[$1],id2firstname[$1]="",id2id[$1]="",id2lastname[$1]=""}
Which produces the following output:
Harry Vickers 768774564650
Steve Simons 76867790987
Mary Simons 0987768798980
Mary
Rick Holister 87879655456
Martin Peterson 87686765766
I would be pleased to know why this removed the last name and ID number of the duplicate record, but left the first name.
Apologies if the technique is odd or unconventional. I have not been learning for long.
If my attempt cannot be fixed or you feel there is a better way, I am happy for you to produce the desired result in a different way, but please:
- use GAWK (as I want to progress with it),
- try to keep it as simple as possible,
- and, explain how it works so I can learn something.
{
, before}
, after,
and;
, etc. And don’t write lines that are longer that the terminal is wide; break them into multiple lines. – Scott Nov 18 '20 at 21:51