如何用文本文件中的相应值替换JSON文档中的数字

时间:2018-12-11 09:45:21

标签: json merge edit jq

我有一个config文件,看起来像这样:

{
    "reads": {
        "no_Reads": 5000000,

        "length": 150,

        "insert_length": 300,

        "insert_sd": 10
    },

    "no_Samples" : 20,

    "species_dist_Params": {
        "mean_log_Mean": 1.0,

        "sd_log_Mean": 0.25,

        "k_log_Sd": 1.0,

        "theta_log_Sd": 1.0,

        "beta": 1.0,

        "alpha": 1.0
    },

    "no_Species" : 100,

    "species": [
        {
            "dir": "Strain_35814/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_285/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_438/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1833/",
            "nStrains": "3"
        },
        {
            "dir": "Strain_782/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1509/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_2095/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_85698/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_382/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_2096/",
            "nStrains": "3"
        },
        {
            "dir": "Strain_1582/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1245/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1452/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_1613/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_43080/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_29519/",
            "nStrains": "3"
        },
        {
            "dir": "Strain_96345/",
            "nStrains": "3"
        },
        {
            "dir": "Strain_777/",
            "nStrains": "3"
        },
        {
            "dir": "Strain_375/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_168695/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_236/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_358/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_399/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_109790/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1718/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_2252/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_729/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1334/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1624/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_29484/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_636/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1408/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1402/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_2287/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_582/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_57975/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_38323/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_2039/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_486/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_738/",
            "nStrains": "1"
        },

        {
            "dir": "Strain_305/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_1580/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1542/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_553/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_774/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_83560/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_715/",
            "nStrains": "3"
        },
        {
            "dir": "Strain_1502/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_948/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_54291/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_480/",
            "nStrains": "3"
        },

        {
            "dir": "Strain_1349/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_83558/",
            "nStrains": "3"
        },
        {
            "dir": "Strain_366648/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_747/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_750/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_837/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1911/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1238/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_644/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_196/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_552/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_40576/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_28025/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_43306/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_663/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_29447/",
            "nStrains": "5"
        },
        {
            "dir": "Strain_546/",
            "nStrains": "3"
        },
        {
            "dir": "Strain_48296/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_1308/",
            "nStrains": "5"
        },
        {
            "dir": "Strain_1398/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_1338/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_770/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_9/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_75985/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_1406/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_33970/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_587753/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_39152/",
            "nStrains": "5"

        },

        {
            "dir": "Strain_2208/",
            "nStrains": "5"
        },

        {
            "dir": "Strain_2209/",
            "nStrains": "5"
        },

        {
            "dir": "Strain_2162/",
            "nStrains": "2"
        },

        {
            "dir": "Strain_43687/",
            "nStrains": "1"
        },

        {
            "dir": "Strain_2173/",
            "nStrains": "1"
        },

        {
            "dir": "Strain_1219/",
            "nStrains": "1"
        },

        {
            "dir": "Strain_28173/",
            "nStrains": "2"
        },

        {
            "dir": "Strain_1598/",
            "nStrains": "1"
        },

        {
            "dir": "Strain_1358/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_714/",
            "nStrains": "4"
        },
        {
            "dir": "Strain_34085/",
            "nStrains": "5"
        },
        {
            "dir": "Strain_1076/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_1270/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_2371/",
            "nStrains": "5"
        },
        {
            "dir": "Strain_1681/",
            "nStrains": "5"
        },
        {
            "dir": "Strain_28197/",
            "nStrains": "3"
        },
        {
            "dir": "Strain_28108/",
            "nStrains": "5"
        },
        {
            "dir": "Strain_1744/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_300/",
            "nStrains": "2"
        },
        {
            "dir": "Strain_160/",
            "nStrains": "5"
        },
        {
            "dir": "Strain_316/",
            "nStrains": "1"
        }

    ],

    "max_seqs" : 5,

    "min_div"  : 0.01,

    "max_div"  : 10.0,

    "min_scg"  : 35
}

我有一个文件a2看起来像这样:

1
1
1
2
3
1
3
1
3
4
4
2
1
2
5
1
2
5
2
1
1
2
2
1
2
2
1
3
1
1
1
1
2
2
1
7
1
2
1
1
4
1
1
1
5
1
2
1
3
3
1
1
1
1
3
1
1
1
1
4
1
1
1
6
1
1
4
2
1
1
1
1
1
9
4
1
2
1
2
2
2
2
3
1
3
1
2
2
1
3
4
1
2
3
1
3
2
1
6
7

我想用文件"nStrains":中的行替换a2之后的数字。所需的输出是:

{
            "dir": "Strain_35814/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_285/",
            "nStrains": "1"
        },
        {
            "dir": "Strain_438/",
            "nStrains": "1"
        ...

我尝试过

awk 'NR==FNR {a[$1];next}/nStrains/{$2="a"}1' a2 FS=":" config > config_new

但似乎不起作用。我想我已经完成了数组部分的修改。我该如何解决?它不起作用的原因是什么?非常感谢你。

2 个答案:

答案 0 :(得分:3)

这是一种减少精简的解决方案。注意,因为使用了inputs,所以需要-n选项。使用-R选项主要是因为要求隐式指定替换值应为字符串,而且还出于健壮性考虑:

调用:

jq -Rn --argfile config config.json -f update.jq a2

update.jq

[inputs] as $dict
| $config
| .species |= [to_entries[] | (.value.nStrains = $dict[.key]) | .value]

答案 1 :(得分:2)

最好使用来处理JSON数据。

在下面的代码中,我使用了reduce,因为with_entries不适用于数组。

jq '.species |= (
        to_entries | map(.value.nStrains = $repl[.key]) |
        reduce .[] as $item ([]; .[$item.key] = $item.value)
    )' --argfile repl a2 config

有关更多信息,请参见man jq