上JSON更新值文件上的文件B A使用参考(Updating Values on json file

2019-10-22 00:05发布

我不知道我怎么能做到这一点,但我有50 / 60K线结构如下文件:

{test_id: 12345, test_name: '', test_time: 213123}
{test_id: 12346, test_name: '', test_time: 331233}

而且,我有每个国家的参考ID第二个文件:

{test_id: 12345, test_name: 'test_a'}
{test_id: 12346, test_name: 'test_b'}

因此,使用为test_id作为参考,这将是跨越,以更新从文件A的TEST_NAME场这两个文件的最有效的方法是什么?

预计穿越的ID后的结果:

{test_id: 12345, test_name: 'test_a', test_time: 213123}
{test_id: 12346, test_name: 'test_b', test_time: 331233}

使用SED或AWK将是实现这一目标的方式首选。

Answer 1:

$ cat tst.awk
{ match($0,/'[^']*'/) }
NR==FNR { id2name[$2] = substr($0,RSTART,RLENGTH); next }
{ print substr($0,1,RSTART-1) id2name[$2] substr($0,RSTART+RLENGTH) }

$ awk -f tst.awk fileA fileB
{test_id: 12345, test_name: 'test_a', test_time: 213123}
{test_id: 12346, test_name: 'test_b', test_time: 331233}


Answer 2:

如果第一个文件名是具有除测试名称的所有信息TEMP.TXT,第二个文件名是temp1.txt具有文本名称信息,下面的命令将做你想做的,并存储在res.txt结果文件。

  awk 'BEGIN 
{ while(getline < "temp1.txt" ) { codes[$2] = substr($4, 0, length($4)-1) } } 
{ printf "%s %s %s %s, %s %s \n",  $1, $2 , $3, codes[$2] , $5 , $6 }'  temp.txt > res.txt


文章来源: Updating Values on json file A using reference on file B
标签: linux bash awk sed