使用Shell脚本合并两个属性文件

11

如何合并两个属性文件,例如使用Shell脚本:- 如果我有两个类似的属性文件

first.properties
/test/file="anish"
/test/version=3.0

second.properties
/test/author=nath
/test/version=2.0

如果我将first.properties合并到second.properties中,则共有的属性应该从first.properties中获取,因此我的输出应该如下所示

final.properties
/test/file="anish"
/test/version=3.0
/test/author=nath
2个回答

21

另一种方法:

$ awk -F= '!a[$1]++' first.properties second.properties

此 awk 命令的输入是第一个文件的内容,后跟第二个文件的内容。 !a[$1]++ 仅打印特定键的第一次出现,从而删除第二个文件中出现的重复项。


2
$ cat first.properties second.properties | awk -F= '!($1 in settings) {settings[$1] = $2; print}'
/test/file="anish"
/test/version=3.0
/test/author=nath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接