使用文件 B 的引用更新 json 文件 A 的值
Updating Values on json file A using reference on file B
我不确定如何实现这一点,但我有一个具有以下结构的 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 来实现。
如果第一个文件名为 temp.txt 包含除测试名称以外的所有信息,第二个文件名为 temp1.txt 包含文本名称信息,则以下命令将执行您想要的操作并存储res.txt.
中的结果文件
awk 'BEGIN
{ while(getline < "temp1.txt" ) { codes[] = substr(, 0, length()-1) } }
{ printf "%s %s %s %s, %s %s \n", , , , codes[] , , }' temp.txt > res.txt
$ cat tst.awk
{ match([=10=],/'[^']*'/) }
NR==FNR { id2name[] = substr([=10=],RSTART,RLENGTH); next }
{ print substr([=10=],1,RSTART-1) id2name[] substr([=10=],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}
我不确定如何实现这一点,但我有一个具有以下结构的 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 来实现。
如果第一个文件名为 temp.txt 包含除测试名称以外的所有信息,第二个文件名为 temp1.txt 包含文本名称信息,则以下命令将执行您想要的操作并存储res.txt.
中的结果文件 awk 'BEGIN
{ while(getline < "temp1.txt" ) { codes[] = substr(, 0, length()-1) } }
{ printf "%s %s %s %s, %s %s \n", , , , codes[] , , }' temp.txt > res.txt
$ cat tst.awk
{ match([=10=],/'[^']*'/) }
NR==FNR { id2name[] = substr([=10=],RSTART,RLENGTH); next }
{ print substr([=10=],1,RSTART-1) id2name[] substr([=10=],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}