我們有兩個文件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