Compare first column of 2 files and replace

Hi All,
I have 2 files in the following format :

File 1

S00999999|BHANU|TEST|007 JOHN DOE APT 999||VENGA HIGHWAY|MA|09566|SCO DUAL|20140201|20140331|20140401|20140630|20140327|
S00888888|BU|TES|009 JOHN DOE APT 909||SENGA HIGHWAY|MA|08566|SCO DUAL|20140301|20140430|20140501|20140731|20140327|
S00888876|BUT|TESY|004 JOHN DOE APT 919||BENGA HIGHWAY|MA|06566|SCO DUAL|20131201|20140131|20140201|20140430|20140327|

File 2 :

S00375383|GEORGIA|KAMELAKIS|PO BOX 705||YARMOUTHPORT|MA|02675|SCO DUAL|20140201||20140331|20140401|20140228|
S00999999|BHANU|TEST|007 JOHN DOE APT 999||VENGA HIGHWAY|MA|09566|SCO DUAL|20140201||20140331|20140401|20131027|
S00888876|BUT|TESY|004 JOHN DOE APT 919||BENGA HIGHWAY|MA|06566|SCO DUAL|20140201||20140331|20140401|20131027|

I need to compare 1st column of both files and replace all values from File 1 into File 2 where they match and also print the original lines of non-matching columns.

Output file :

S00375383|GEORGIA|KAMELAKIS|PO BOX 705||YARMOUTHPORT|MA|02675|SCO DUAL|20140201||20140331|20140401|20140228|
S00999999|BHANU|TEST|007 JOHN DOE APT 999||VENGA HIGHWAY|MA|09566|SCO DUAL|20140201|20140331|20140401|20140630|20140327|
S00888876|BUT|TESY|004 JOHN DOE APT 919||BENGA HIGHWAY|MA|06566|SCO DUAL|20131201|20140131|20140201|20140430|20140327|

I tried this through different awk commands but did not help :

awk 'FNR==NR{a[$0];next} !($1 in a)' file1 file 2

awk 'FNR==NR { a[$1]=$2; next } $1 in a { print $1, a[$1], $2, $3, $4, $5, $6, $7 }' file1 file 2

Any help would be appreciated.

Try this:

awk -F\| 'NR==FNR {a[$1]=$0; next} $1 in a {print a[$1]; next}1' file1 file2
1 Like