我想組合 2 個包含用于字串本地化的鍵值對的檔案。我應該保留檔案 1 中的附加值,并重寫檔案 2 中的更新值。
檔案 1:
"cart.title" = "Cart";
"cart.subtitle" = "Price";
"checkout.title" = "Checkout";
檔案 2:
"cart.title" = "Cart";
"checkout.title" = "Super checkout";
期望的結果:
"cart.title" = "Cart";
"cart.subtitle" = "Price";
"checkout.title" = "Super checkout";
我已經用 awk 嘗試過,但問題是它總是將新字串從檔案 1 移動到檔案末尾。而且我想將我的字串分組為原始檔案。
awk -F= '!a[$1] ' 2.strings 1.strings > results.strings
但我正在努力維持原始順序,結果如下所示:
"cart.title" = "Cart";
"checkout.title" = "Super Checkout";
"cart.subtitle" = "Price";
我認為對于任何使用 Linux 的人來說,這應該是一件很容易的事。提前致謝。
uj5u.com熱心網友回復:
使用您顯示的示例和嘗試,請嘗試以下awk代碼。
awk '
FNR==NR{
arr1[$1]=$0
arr2[ count]=$1
next
}
($1 in arr1){
arr1[$1]=$0
}
END{
for(i=1;i<=count;i ){
print arr1[arr2[i]]
}
}
' file1 file2
說明:為上述代碼添加詳細說明。
awk ' ##Starting awk program from here.
FNR==NR{ ##Checking condition which will be TRUE when file1 is being read.
arr1[$1]=$0 ##Creating arr1 with index of $1 and value of $0.
arr2[ count]=$1 ##Creating arr2 with index of count and value of $0.
next ##next will skip all further statements from here.
}
($1 in arr1){ ##Checking condition if $1 is in arr1 then do following.
arr1[$1]=$0 ##Setting arr1 value to $0 with index of $1.
}
END{ ##Starting END block of this program from here.
for(i=1;i<=count;i ){ ##Starting a for loop from 1 to till value of count.
print arr1[arr2[i]] ##Printing value of arr1 with index of arr2 here.
}
}
' file1 file2 ##Mentioning Input_file names here.
uj5u.com熱心網友回復:
awk '
NR==FNR { map[$1]=$0; next }
{ print ($1 in map ? map[$1] : $0) }
' file2 file1
"cart.title" = "Cart";
"cart.subtitle" = "Price";
"checkout.title" = "Super checkout";
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/466158.html
