【发布时间】:2016-04-25 12:33:04
【问题描述】:
我们有两个文件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 中的 cmets。这应该是 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 代码。