我建議使用python
作爲任務。考慮下面這個例子:
import json
from pprint import pprint
fileName1 = "file1.json" # file1 contains something like { "Testname": "color", "result": "fail" }
fileName2 = "file2.json" # file2 contains something like { "AnotherTestname": "color2", "result": "pass" }
fileNameTo = "file3.json"
def visualize(data, prompt):
print(prompt)
pprint(data)
raw_input("Press <Enter> to continue: ") # this line pauses the execution
def loadData(fname):
with open(fname, "r") as f:
return json.load(f)
jd1 = loadData(fileName1)
visualize(jd1, "Data from %s" % fileName1)
jd2 = loadData(fileName2)
visualize(jd2, "Data from %s" % fileName2)
jd3 = jd1.copy() # create a copy of data from file1, this step can be avoided if you don't need unmodified jd1
visualize(jd3, "Data after copying")
jd3.update(jd2) # merge copy of data from file1 with file2, updating corresponding keys
visualize(jd3, "Data after merging")
with open("file3.json", "w") as f3:
json.dump(jd3, f3)
這個腳本的版本取得了一定的「互動」,它讓你在執行的每一步控制數據的狀態。我剛剛檢查過它按預期執行數據合併。請嘗試根據您的測試數據運行它並查看輸出。
請提供更多關於輸入和預期輸出的信息。 – luoluo
對不起,以下是更多信息:file1.json包含以下Testname:color「result」:「fail」,並且file2.json包含以下內容:Testname:color「result」:「pass」,並且我預計file3.json包含與最後結果相同的行,例如:Testname:color「result」:「pass」,這是因爲file2.json爲什麼更新 – shaveax
是awk的一個要求嗎?其實它似乎有點尷尬:) – user3159253