组合键值对变化的脚本

Script for combining changes in key value pairs

我想合并 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[]++' 2.strings 1.strings > results.strings

但是我很难维持原来的顺序,结果是这样的:

"cart.title" = "Cart";

"checkout.title" = "Super Checkout";
"cart.subtitle" = "Price";

我认为对于域为 Linux 的任何人来说,这应该是非常容易的任务。 提前致谢。

根据您展示的示例和尝试,请尝试遵循 awk 代码。

awk '
FNR==NR{
  arr1[]=[=10=]
  arr2[++count]=
  next
}
( in arr1){
  arr1[]=[=10=]
}
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[]=[=11=]               ##Creating arr1 with index of  and value of [=11=].
  arr2[++count]=          ##Creating arr2 with index of ++count and value of [=11=].
  next                      ##next will skip all further statements from here.
}
( in arr1){               ##Checking condition if  is in arr1 then do following.
  arr1[]=[=11=]               ##Setting arr1 value to [=11=] with index of .
}
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.
awk '
    NR==FNR { map[]=[=10=]; next }
    { print ( in map ? map[] : [=10=]) }
' file2 file1
"cart.title" = "Cart";
"cart.subtitle" = "Price";

"checkout.title" = "Super checkout";