我不知道我怎么能做到这一点,但我有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将是实现这一目标的方式首选。
$ 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}
如果第一个文件名是具有除测试名称的所有信息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