Simply_me
Simply_me

Reputation: 2970

Finding The Delta In Properties Between two files

This is primarily an approach/concept question, and I appreciate any input you might have.

The problem: I often need to compare and edit a property file, and in doing so I'm mainly interested to find new properties that exist in the new file compared to the old one. To achieve this I often employ diff old_file.prop new_file.prop,but due to the high number of lines/properties in each file (~150) this method isn't efficient and is error prone.

Sample old_file.prop:

name.host=mycomputer1
internal.port=21
external.gateway=sample.my.machine

Sample new_file.prop:

name.host=change_me
internal.port=21
external.gateway=change_me
external.port=501

Here diff command will return:

<name.host=mycomputer1
<external.gateway=sample.my.machine
>name.host=change_me
>externa.gateway=change_me
>external.port=501

The only output of interest/desired in this example is external.port and not the value it holds (and perhaps a line number as well). I'm a bit familiar with sed, but I don't think it can do this without prior knowledge of the properties in the new file.

Is there a way to efficiently achieve this using bash script?

Thank you,

Upvotes: 2

Views: 425

Answers (3)

captcha
captcha

Reputation: 3756

Code for GNU :

awk -F= 'NR==FNR {a[$1]++;next}; !a[$1] {print $1, "line", FNR}' fileOld fileNew

Upvotes: 4

twalberg
twalberg

Reputation: 62379

As an alternative on @mob's answer, and to give some other possibilities:

  1. To show properties that have been removed in new_file

    comm -23 <(cut -d= -f1 old_file) <(cut -d= -f1 new_file)

  2. To show properties that have been added in new_file

    comm -13 <(cut -d= -f1 old_file) <(cut -d= -f1 new_file)

  3. To show common properties in the two files

    comm -22 <(cut -d= -f1 old_file) <(cut -d= -f1 new_file)

Upvotes: 2

mob
mob

Reputation: 118605

You could use cut to ignore any input after the first = on each line:

diff <(cut -d= -f1 old_file) <(cut -d= -f1 new_file)

Projected output:

22a23
> external.port

Upvotes: 1

Related Questions