Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two properties file using shell scripts

Tags:

bash

shell

unix

How to merge two properties file, using shell scripts for example : - if i have two properties file like

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

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

if i merge first.properties over second.properties then common existing property should taken from the first.properties so my output should look like

final.properties
/test/file="anish"
/test/version=3.0
/test/author=nath
like image 999
anish Avatar asked Dec 24 '12 05:12

anish


2 Answers

Another way:

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

The input to this awk is the content of first file followed by second file. !a[$1]++ prints only the first occurence of a particular key, hence removing duplicates apparing in the 2nd file.

like image 108
Guru Avatar answered Oct 25 '22 01:10

Guru


$ cat first.properties second.properties | awk -F= '!($1 in settings) {settings[$1] = $2; print}'
/test/file="anish"
/test/version=3.0
/test/author=nath
like image 37
John Kugelman Avatar answered Oct 25 '22 00:10

John Kugelman