Rashid Rashid - 7 months ago 7
Bash Question

Appending contents to a file if doesn't exist using comma values

We have a file named parameters.txt which contains in comma separated value:

db.hostname,db.username,db.password,db.url,db.status,codesettingsA, codesettimgsB


Also, we have a main file called properties.txt which contains in Line format

db.hostname=hostname
db.username=username
db.password=password


I need to pass the comma separated values in parameters.txt and check whether if it is exist in properties.txt. If it is not exist it should print bottom of the properties.txt

For Example, in the properties.txt: db.url, db.status, codesettingsA and codesettimgsB is not there. However, I’m passing from the parameters.txt. It should be appended and the result should be like this in properties.txt:

db.url
db.status
codesettingsA
codesettimgsB


Note: Already values are declared for db.hostname=hostname, db.username=username and db.password=password in properties file. I'm passing db,hostname,db.username,db.password from parameter file. So, this should ignore the values even after the comma and shouldn't print db,hostname,db.username,db.password

Is there any ways to print them at the bottom page of properties.txt?
Can you provide sample code?

Answer

You can use this awk:

awk 'FNR==NR{for (i=1; i<=NF; i++) a[$i]; next} $1 in a{delete a[$1]} 1;
   END{for (i in a) print i "="}' FS=' *, *' params.txt FS=' *= *' props.txt

Output:

db.hostname=hostname
db.username=username
db.password=password
codesettimgsB=
db.url=
codesettingsA=
db.status=