在JSON更新值文件的文件B A使用的参考 [英] Updating Values on json file A using reference on file B

查看:92
本文介绍了在JSON更新值文件的文件B A使用的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我怎么能做到这一点,但我有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作为参考,这将是跨越为了从文件中的更新TEST_NAME场这两个文件的最有效的方法是什么?

穿越的ID后的预期结果:

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

使用awk或者sed将是prefered的方式来实现这一目标。


解决方案

  $猫tst.awk
{匹配($ 0 /'[^'] *'/)}
NR == FNR {id2name [$ 2] = SUBSTR($ 0 RSTART,RLENGTH);下一个 }
{打印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}

I'm not sure how i could achieve this, but i have a file with 50/60k lines with the following structure:

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

And, i have this second file with the reference id for each country:

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

so, Using the test_id as reference, what would be the most effective way to cross those two files in order to update the test_name field from file A ?

Expected result after crossing id's:

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

Using sed or awk would be prefered way to achieve this.

解决方案

$ 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}

这篇关于在JSON更新值文件的文件B A使用的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆