2016-04-25 161 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