追加内容到一个文件,如果不存在使用逗号值 [英] Appending contents to a file if doesn't exist using comma values

查看:218
本文介绍了追加内容到一个文件,如果不存在使用逗号值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个名为parameters.txt文件,其中包含逗号分隔值:

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

另外,我们有一个主文件名为的properties.txt其中包含行格式

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

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

我需要通过逗号分隔值parameters.txt并检查是否如果是的properties.txt存在。如果不存在,它应该打印的properties.txt底部

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

例如,在的properties.txt:db.url配置参数,db.status,codesettingsA和codesettimgsB是不存在的。不过,我从parameters.txt传递。它应该被追加和结果应该是像这样的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

请注意:已值申报db.hostname =主机名,db.username =在属性文件中的用户名和db.password =密码。我路过分贝,主机名,db.username,db.password从参数文件。所以,这甚至应该逗号后忽略值,不应打印分贝,主机名,db.username,db.password

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

时那里的properties.txt底部页打印他们的任何方式?
你能否提供样品code?

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

推荐答案

您可以使用此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

输出:

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

这篇关于追加内容到一个文件,如果不存在使用逗号值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆