File Compare at field level

Hi,

I am trying to compare two fixed width files as shown below. The file is NOT sorted. The field in bold red is the key field. The comparison needs to be based of key fields and not whole record. But needs to write out the whole record in the output.


OldFile.txt:
A100135123456789  firstname mi lastname .......
A100112123456677  firstname mi lastname .......
A189135123456777  firstname mi lastname .......

NewFile.txt:
A100135123456789  firstname mi lastname .......
B122112123456666  firstname mi lastname .......
C123135123456444  firstname mi lastname .......

I need to compare the Old file and New file based on their key field in boldred (not the whole record) and generate two files: NewRec.txt and DropRec.txt. These two files will contain the whole record even though the comparison is done only on the key fields.

NewRec.txt: Key field that was not there in Old file but exists in New file. So comparing the key fields in the file above, the output should be
B122112123456666 firstname mi lastname .......
C123135123456444 firstname mi lastname .......

DropRec.txt: Key field that was there in Old file but NOT in new file. So comparing the key fields in the file above, the output should be:
A100112123456677 firstname mi lastname .......
A189135123456777 firstname mi lastname .......

The comparison is on the key field and not the whole file. The file is fixed width. The key field is from character 1 thru 22. Even though the text is only there from char 1 thru 16 and padded with spaces till 22 character.

Will really appreciate any help.

Thanks

awk 'NR==FNR{A[$1];next}($1 in A)' OldFile.txt NewFile.txt

awk 'NR==FNR{A[$1];next}!($1 in A)' OldFile.txt NewFile.txt

You can modify the sequence of input files to get desired results.

Thanks for suggestion. I tried the suggested code but somehow getting the error

Below is the code I tried:

#!/bin/ksh

pfile=oldfile.txt
cfile=currentfile.txt

awk 'NR==FNR{A[$1];next}($1 in A)' ${pfile} ${cfile} #> NewRec.txt

awk 'NR==FNR{A[$1];next}!($1 in A)' ${pfile} ${cfile} #> OldRec.txt
The error I am getting is:
awk: syntax error near line 1
awk: bailing out near line 1
awk: syntax error near line 1
awk: bailing out near line 1

Thanks

Use nawk or /usr/xpg4/bin/awk instead on Solaris / SunOS

Thank you so much....the code below is working perfectly now.

nawk 'NR==FNR{A[$1];next}!($1 in A)' ${pfile} ${cfile} > NewRec.txt
nawk 'NR==FNR{A[$1];next}!($1 in A)' ${cfile} ${pfile} > DropRec.txt

I am a little beginner in awk. I wanted to check how the nawk statement above is picking up the first field for comparison. Also, just trying to break down the nawk statement above to understand how it is working.

Thanks

The program reads first file and stores the value of first field as key in Associate Array: A

The program then reads second file and check if first field is not a key in Associate Array: A and print the whole record if true.