Replace text from one file in another file

I have 2 files. Here is a sample from file 1.

NETIK0102_UCS_Boot 20000025b510105d
NETIK0102_UCS_Boot 20000025b510104d
NETIK0102_UCS_HBA0 20000025b510113e
NETIK0102_UCS_HBA1 20000025b510112e
NETIK0102_UCS_HBA2 20000025b51010de
NETIK0102_UCS_HBA3 20000025b51010fe
SEIADWFMPRD1 10000000c962521c
SEIADWFMPRD1 10000000c96253ab
SEIDEVDB61 10000000c9ae12ec
SEIDEVDB61 10000000c9a8c842

And here is a sample from file 2

NETIK0102 195702363 19BB
NETIK0102 195702363 19BC
NETIK0102 195702363 1AA3
NETIK0102 195702363 1A94
NETIK0102 195702363 1A88
SEIADWFMPRD1 195702363 1992
SEIADWFMPRD1 195702363 1993
SEIADWFMPRD1 195702363 19B1
SEIADWFMPRD1 195702363 19C4
SEIDEVDB61 195702363 1726
SEIDEVDB61 195702363 1727
SEIDEVDB61 195702363 1728
SEIDEVDB61 195702363 1729
SEIDEVDB61 195702363 172A

Where ever the first column in file2 is like the first column in file1 I need to replace replace the replace the first column in file2 with the column 2 of file 1. So my output would look like

 
20000025b510105d 195702363 19BB
20000025b510105d  195702363 19BC
20000025b510105d  195702363 1AA3
20000025b510105d  195702363 1A94
20000025b510105d  195702363 1A88
20000025b510104d  195702363 19BB
20000025b510104d 195702363 19BC
20000025b510104d 195702363 1AA3
20000025b510104d 195702363 1A94
20000025b510104d 195702363 1A88
....
....
....
10000000c9ae12ec 195702363 1726
10000000c9ae12ec 195702363 1727
10000000c9ae12ec 195702363 1728
10000000c9ae12ec 195702363 1729
10000000c9ae12ec 195702363 172A
10000000c9a8c842 195702363 1726
10000000c9a8c842 195702363 1727
10000000c9a8c842 195702363 1728
10000000c9a8c842 195702363 1729
10000000c9a8c842 195702363 172A
 

Any tips on how to do this using awk, grep, sed of nested while looks would be much appreciated.

Try,

awk 'NR==FNR {a[$1]=$2;next} ($1 in a) {print a[$1] FS $2}' file1 file2

Not tested though.

awk 'NR==FNR{arr[$1]=$2;next}($1 in arr){$1=arr[$1]}1' file1 file2

Thanks it almost works. But is there a way to use ~ instead of = ? So this way it will swap all the occurences of NETIK0102_UCS_BOOT, and NETIK0102_UCS_HBA0 etc? If it matches any of the word NETIK0102 in column 1 I would want to replace it with the each corresponding alphanumeric string in column 2 that has NETIK0102 in file2?

Thanks!

Try:

awk 'NR==FNR{arr[$1]=$2;next}{for(ele in arr) {if ($1 ~ ele) {$1=arr[ele]}}}1' file1 file2
1 Like