2016-04-25 138 views
2

我们有两个文件FILE1.TXT和FILE2.TXT添加注释到文件

FILE1.TXT

PropertyA 
PropertyB 
PropertyC 
####Some Comments## 
PropertyA 
PropertyB 
PropertyC 
PropertyD 

FILE2.TXT

#This is Property A 
PropertyA=valueforpropertyA 

PropertyB=valueforpropertyB 
#Adding values to Property C 
PropertyC=valueforpropertyC 
#Value for Property D 
PropertyD=valueforpropertyD 
#This is Property E 
PropertyE=valueforpropertyE 

PropertyF=valueforpropertyF 
#End of Properties 
#End of values 
#End of Files 

使用我们用来写如下的命令在file1.txt的#### Some Comments ##部分中出现的属性之后,从file2.txt中的值file1.txt。

awk -F'=' 'FNR==NR{if (p) a[$0]; else {print; if ($0 ~ /####Some Comments##/) p=1} next} 
    $1 in a' file1.txt file2.txt > _file1.txt && mv _file1.txt file1.txt 

这是输出:

PropertyA 
PropertyB 
PropertyC 
####Some Comments## 
PropertyA=valueforpropertyA 
PropertyB=valueforpropertyB 
PropertyC=valueforpropertyC 
PropertyD=valueforpropertyD 

我们需要上面的命令打印出是出现在FILE2.TXT也评论。这应该是file1.txt的输出

*PropertyA 
PropertyB 
PropertyC 
####Some Comments## 
#This is Property A 
PropertyA=valueforpropertyA 
PropertyB=valueforpropertyB 
#Adding values to Property C 
PropertyC=valueforpropertyC 
#Value for Property D 
PropertyD=valueforpropertyD 
#End of Properties 
#End of values 
#End of Files* 

如何使用上述命令完成此操作?

+0

一些模糊的awk码。 – SaintHax

回答

0
awk -F'=' 'FNR==NR{if (p) a[$0]; else {print; if ($0 ~ /####Some Comments##/) p=1} next} { if($1 in a) { print a[$1] } else if ($1 ~ /^#/){ print }}' file1.txt file2.txt > _file1.txt && mv _file1.txt file1.txt 

试试这个....

我只是增加了额外的支架,打印阉了一个阵列的行包含$ 1或没有(如果没有,其评论)

+0

嗨乔达, 感谢您的回复, 我试过了你的命令。但是,获取错误消息意外的换行符或字符串结尾// {print} // awk:cmd。行:1:FNR == NR {if(p)a [$ 0]; else {print; if($ 0〜/ #### Some Comments ## /)p = 1} next} {if($ 1 in a){print a [$ 1]} else if($ 1〜/ ^#/){print} awk :cmd。行:1:^意外的换行符或字符串结尾 – Rashid

+0

这是我的命令:awk -F'=''FNR == NR {if(p)a [$ 0]; else {print; if($ 0〜/ #### Some Comments ## /)p = 1} next} {if($ 1 in a){print a [$ 1]} else if($ 1〜/ ^#/){print}'file1 .txt file2.txt> _file1.txt && mv _file1.txt file1.txt。我该如何解决这个问题? – Rashid

+0

Ups,我在最后错过了一个“}”:)我编辑了我的答案 – Joda