在 UNIX shell 脚本中提取 json 的一部分并附加一个 属性
Extract a part of json and append a property in UNIX shell scripting
我有一个 json,我需要从中提取一部分并 append/insert 一个新值到现有数组中。我的代码如下,
{
"itemId": "item_1",
"itemName": "item 1",
"itemPosition": [{
"posId": "item_1_1",
"rowPos": 0,
"columnPos": 0
}, {
"posId": "item_1_2",
"rowPos": 0,
"columnPos": 1
}, {
"posId": "item_1_3",
"rowPos": 1,
"columnPos": 0
}, {
"posId": "item_1_4",
"rowPos": 1,
"columnPos": 1
}]
}, {
"itemId": "item_2",
"itemName": "item 2",
"itemPosition": [{
"posId": "item_2_1",
"rowPos": 0,
"columnPos": 0
}, {
"posId": "item_2_2",
"rowPos": 0,
"columnPos": 1
}, {
"posId": "item_2_3",
"rowPos": 1,
"columnPos": 0
}, {
"posId": "item_2_4",
"rowPos": 1,
"columnPos": 1
}]
}
在这个 json 中,我需要在项目 ID "item_1" 下的 itemPosition 中添加一个新值,例如
{
"posId": "item_1_5",
"rowPos": 2,
"columnPos": 1
}
我使用以下命令提取 json 使用字符串搜索
猫sample.json |荷兰 | sed -n '/"item_1"/,/"item_2"/p'
输出是:
2 "itemId": "item_1",
3 "itemName": "item 1",
4 "itemPosition": [{
5 "posId": "item_1_1",
6 "rowPos": 0,
7 "columnPos": 0
8 }, {
9 "posId": "item_1_2",
10 "rowPos": 0,
11 "columnPos": 1
12 }, {
13 "posId": "item_1_3",
14 "rowPos": 1,
15 "columnPos": 0
16 }, {
17 "posId": "item_1_4",
18 "rowPos": 1,
19 "columnPos": 1
20 }]
21 }, {
22 "itemId": "item_2",
问题是我应该如何遍历 itemPosition 数组以找到数组中的最后一个值并在其后追加或插入一个新值?
您可以使用jq解析shell中的json。 jq 可以选择性地将字符串附加到 json 文件。
这段代码将实现您想要做的事情:
cat your.json | jq '.itemPosition |= . + [{"posId":"item_1_5","rowPos": 2,"columnPos": 1}]'
编辑:这仅适用于从第 22 行开始中断的 json 文件的第 1 到 21 行。
如果您的 linux 框中没有安装 jq 或任何解析器,则可以使用以下代码。它应该适合你的要求。请检查。
line_to_be_replaced=`cat itemlist.json | nl | sed -n '/"item_1"/,/"item_2"/p' | grep -in "}]" | awk '{print }'`
sed -i "${line_to_be_replaced} s|.*|}, {\n\"posId\": \"item_1_5\",\n\"rowPos\": 2,\n\"columnPos\": 1\n}]|g" itemlist.json
注意:itemlist.json 文件包含 JSON 代码。
我有一个 json,我需要从中提取一部分并 append/insert 一个新值到现有数组中。我的代码如下,
{
"itemId": "item_1",
"itemName": "item 1",
"itemPosition": [{
"posId": "item_1_1",
"rowPos": 0,
"columnPos": 0
}, {
"posId": "item_1_2",
"rowPos": 0,
"columnPos": 1
}, {
"posId": "item_1_3",
"rowPos": 1,
"columnPos": 0
}, {
"posId": "item_1_4",
"rowPos": 1,
"columnPos": 1
}]
}, {
"itemId": "item_2",
"itemName": "item 2",
"itemPosition": [{
"posId": "item_2_1",
"rowPos": 0,
"columnPos": 0
}, {
"posId": "item_2_2",
"rowPos": 0,
"columnPos": 1
}, {
"posId": "item_2_3",
"rowPos": 1,
"columnPos": 0
}, {
"posId": "item_2_4",
"rowPos": 1,
"columnPos": 1
}]
}
在这个 json 中,我需要在项目 ID "item_1" 下的 itemPosition 中添加一个新值,例如
{
"posId": "item_1_5",
"rowPos": 2,
"columnPos": 1
}
我使用以下命令提取 json 使用字符串搜索
猫sample.json |荷兰 | sed -n '/"item_1"/,/"item_2"/p'
输出是:
2 "itemId": "item_1",
3 "itemName": "item 1",
4 "itemPosition": [{
5 "posId": "item_1_1",
6 "rowPos": 0,
7 "columnPos": 0
8 }, {
9 "posId": "item_1_2",
10 "rowPos": 0,
11 "columnPos": 1
12 }, {
13 "posId": "item_1_3",
14 "rowPos": 1,
15 "columnPos": 0
16 }, {
17 "posId": "item_1_4",
18 "rowPos": 1,
19 "columnPos": 1
20 }]
21 }, {
22 "itemId": "item_2",
问题是我应该如何遍历 itemPosition 数组以找到数组中的最后一个值并在其后追加或插入一个新值?
您可以使用jq解析shell中的json。 jq 可以选择性地将字符串附加到 json 文件。
这段代码将实现您想要做的事情:
cat your.json | jq '.itemPosition |= . + [{"posId":"item_1_5","rowPos": 2,"columnPos": 1}]'
编辑:这仅适用于从第 22 行开始中断的 json 文件的第 1 到 21 行。
如果您的 linux 框中没有安装 jq 或任何解析器,则可以使用以下代码。它应该适合你的要求。请检查。
line_to_be_replaced=`cat itemlist.json | nl | sed -n '/"item_1"/,/"item_2"/p' | grep -in "}]" | awk '{print }'`
sed -i "${line_to_be_replaced} s|.*|}, {\n\"posId\": \"item_1_5\",\n\"rowPos\": 2,\n\"columnPos\": 1\n}]|g" itemlist.json
注意:itemlist.json 文件包含 JSON 代码。