我们有两个文件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*
如何使用上述命令完成此操作?
一些模糊的awk码。 – SaintHax