简体   繁体   English

在两个文件之间查找属性中的增量

[英]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. 为了实现这一点,我经常使用diff old_file.prop new_file.prop ,但由于每个文件中的行/属性数量很多(~150),因此该方法效率低,容易出错。

Sample old_file.prop : 示例old_file.prop

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

Sample new_file.prop : 示例new_file.prop

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

Here diff command will return: diff命令将返回:

<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). 在此示例中,唯一需要/需要的输出是external.port而不是它拥有的值(也许还有行号)。 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. 我对sed有点熟悉,但如果没有事先了解新文件中的属性,我认为它不能做到这一点。

Is there a way to efficiently achieve this using bash script? 有没有一种方法可以使用bash脚本有效地实现这一目标?

Thank you, 谢谢,

Code for GNU : GNU 代码:

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

As an alternative on @mob's answer, and to give some other possibilities: 作为@mob答案的替代方法,并提供了其他可能性:

  1. To show properties that have been removed in new_file 显示已在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 显示已在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)

You could use cut to ignore any input after the first = on each line: 您可以使用cut忽略每行的第一个=之后的任何输入:

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

Projected output: 预计输出:

22a23
> external.port

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM