需要将数据从一个文本文件追加到Linux中的json文件

时间:2019-03-06 15:08:28

标签: json

file1.txt

{ "somedata": { "common_examples": [ { "text": "yes", "intent": "affirm" }, { "text": "yep", "intent": "affirm" } ], "xyz_synonyms": [], "regex_features": []   } }

file2.json

{      "name" : "jobname",      "pattern":"[A-Z]+-[0-9]"     },     {      "name" : "project",      "pattern":"[A-Z]{4}"    }

输出file.json

{ "somedata": { "common_examples": [ { "text": "yes", "intent": "affirm" }, { "text": "yep", "intent": "affirm" } ], "xyz_synonyms": [], "regex_features": [ {      "name" : "jobname",      "pattern":"[A-Z]+-[0-9]"     },     {      "name" : "project",      "pattern":"[A-Z]{4}"    } ]   } }

1 个答案:

答案 0 :(得分:-2)

使用sed流编辑器

sed "s/\"regex_features\": \[\]/\"regex_features\": \[ $(cat file2.json) \]/g" file1.txt

输出

➜  ~  sed "s/\"regex_features\": \[\]/\"regex_features\": \[ $(cat file2.json) \]/g" file1.txt
{ "somedata": { "common_examples": [ { "text": "yes", "intent": "affirm" }, { "text": "yep", "intent": "affirm" } ], "xyz_synonyms": [], "regex_features": [ { "name" : "jobname", "pattern":"[A-Z]+-[0-9]" }, { "name" : "project", "pattern":"[A-Z]{4}" } ] } }

这可以工作,但可能并不优雅;-)