# File Compare at field level

**URL:** <https://community.unix.com/t/file-compare-at-field-level/339601>\
**Category:** Shell Programming and Scripting\
**Created:** [February 11, 2014, 6:52pm UTC](https://community.unix.com/t/file-compare-at-field-level/339601 "2014-02-11T18:52:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saanvi1](https://community.unix.com/letter_avatar/saanvi1/32/5_5575768a8748004e209b776fc1b2916d.png) [@Saanvi1](https://community.unix.com/u/Saanvi1)\
**Post date:** [February 11, 2014, 6:52pm UTC](https://community.unix.com/t/file-compare-at-field-level/339601/1 "2014-02-11T18:52:31Z")

</div>

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.

```nohighlight

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

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [February 11, 2014, 7:16pm UTC](https://community.unix.com/t/file-compare-at-field-level/339601/2 "2014-02-11T19:16:36Z")

</div>

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![Saanvi1](https://community.unix.com/letter_avatar/saanvi1/32/5_5575768a8748004e209b776fc1b2916d.png) [@Saanvi1](https://community.unix.com/u/Saanvi1)\
**Post date:** [February 12, 2014, 11:00am UTC](https://community.unix.com/t/file-compare-at-field-level/339601/3 "2014-02-12T11:00:03Z")

</div>

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

```nohighlight
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

```

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [February 12, 2014, 11:52am UTC](https://community.unix.com/t/file-compare-at-field-level/339601/4 "2014-02-12T11:52:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saanvi1](https://community.unix.com/letter_avatar/saanvi1/32/5_5575768a8748004e209b776fc1b2916d.png) [@Saanvi1](https://community.unix.com/u/Saanvi1)\
**Post date:** [February 12, 2014, 1:23pm UTC](https://community.unix.com/t/file-compare-at-field-level/339601/5 "2014-02-12T13:23:38Z")

</div>

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

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![Yoda](https://community.unix.com/user_avatar/community.unix.com/yoda/32/2498_2.png) [@Yoda](https://community.unix.com/u/Yoda)\
**Post date:** [February 12, 2014, 1:35pm UTC](https://community.unix.com/t/file-compare-at-field-level/339601/6 "2014-02-12T13:35:04Z")

</div>

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.
