Comparing two files and replacing fields

I have two files with ids and email addresses. File 2 cotains a subset of the records in file 1. The key field is the first field containing the id.

file 1:

123|myadr@abc.com
456|myadr2@abc.com
789|myadr3@abc.com

file 2:

456|adr456@xyz.com

Where the record appears in the second file, I want to replace the email address in the first file with the address in the second file.

For example, output from above two files would be:

123|myadr@abc.com
456|adr456@xyz.com
789|myadr3@abc.com

I know that I can use awk somehow for this but do not know how so any help would be appreciated.

 nawk -F"|" 'NR==FNR{ a[$1]=$0 ;next}
($1 in a) {$0=a[$1]}
1' file2 file1

;);):wink:

Thanks so much! This is exactly what I needed. :):slight_smile:

you are welcome :p:p:p