2012-11-01 104 views
0

我有兩個屬性文件,我想用文件B中的任何匹配鍵/值條目替換文件A中的鍵/值對。文件A將具有比文件B更多的條目 - 預計這兩個文件不會有完全相同的條目數量。此外,文件B可能具有不包含在文件A.屬性文件搜索和替換

作爲一個例子條目:

File A 
"GB" = "United Kingdom"; 
"SE" = "Sweden"; 
"BR" = "Brazil"; 
"FR" = "France"; 
"ES" = "Spain"; 
"DE" = "Germany"; 

File B 
"GB" = "Regno Unito"; 
"SE" = "Svezia"; 
"BR" = "Brasile"; 
"BR" = "Brasile"; 
"CL" = "Cile"; 

Desired Result 
"GB" = "Regno Unito"; 
"SE" = "Svezia"; 
"BR" = "Brasile"; 
"FR" = "France"; 
"ES" = "Spain"; 
"DE" = "Germany"; 
"CL" = "Cile"; 

是否可以執行該搜索和使用bash更換?

感謝,

肖恩

+0

我自己並沒有真正嘗試過一種解決方案。只是在這個問題上尋求指導。 – seanoshea

回答

2

下面是使用GNU awk一個辦法:

awk -F " = " 'FNR==NR { array[$1]=$2; next } $1 in array { sub ($2, array[$1]) }1' fileb filea 

結果:

"GB" = "Regno Unito"; 
"SE" = "Svezia"; 
"BR" = "Brasile"; 
"FR" = "France"; 
"ES" = "Spain"; 
"DE" = "Germany"; 

編輯:

您可以簡單地刪除發生替換後的數組元素。然後,在腳本的末尾,打印出來還剩下什麼:

awk -F " = " 'FNR==NR { array[$1]=$2; next } $1 in array { sub ($2, array[$1]); delete array[$1] }1; END { for (i in array) print i FS array[i] }' fileb filea 

結果:

"GB" = "Regno Unito"; 
"SE" = "Svezia"; 
"BR" = "Brasile"; 
"FR" = "France"; 
"ES" = "Spain"; 
"DE" = "Germany"; 
"CL" = "Cile"; 
+0

** + 1 **。看來你看到的每一個awk文章都包含'FNR == NR'。 :-) – ghoti

+0

@ ghoti:謝謝隊友。你是對的,這是我最喜歡的構造,它的眼睛也很容易:-) – Steve

+1

我只是指出你答案中沒有什麼特別的愚蠢。據我所知,它應該在非GNU awk中工作得很好。 – ghoti

1

下只的bash腳本會吐出你要求的結果:

#!/bin/bash 

# Identify our files. If you want, test for their existence before proceeding. 
fileA="$1" 
fileB="$2" 

# Define an associated array 
declare -A countries 

# Read our initial data 
while read cc junk name; do 
if [[ -n "$cc" ]]; then 
    countries["$cc"]="$name" 
fi 
done < "$fileA" 

# Overwrite array elements with updated values 
while read cc junk name; do 
if [[ -n "$cc" ]]; then 
    countries["$cc"]="$name" 
fi 
done < "$fileB" 

# Print the results 
for cc in "${!countries[@]}"; do 
    echo "$cc = ${countries[$cc]}" 
done 

結果將不會以完全相同的順序,但我懷疑這並不重要。如果是,您可以創建一個索引爲計數器的附加數組,然後您可以簡單地遍歷該數組以獲取正確順序的$countries索引,而不是最終的for cc in ...。讓我知道如果這是重要的,你不能弄清楚。

我只發佈這裏,因爲你問了一個bash解決方案。 Steve的awk腳本要簡潔得多,而且速度可能要快很多。 (只是猜測,它可能不值得花時間進行基準測試。)