I have a .json file where I need to replace a particular value:
{
"partitions": [
{
"filesystem_type": "FAT",
"label": "boot",
"mkfs_options": "-F 32",
"partition_size_nominal": 256,
"uncompressed_tarball_size": 53,
"want_maximised": false,
"sha256sum": "dd54710df7756e64cff43bba3c930e85c97f22d43e0d32bc37396be34166c148"
},
{
"filesystem_type": "ext4",
"label": "root",
"mkfs_options": "-O ^huge_file",
"partition_size_nominal": 1415,
"uncompressed_tarball_size": 1015,
"want_maximised": true,
"sha256sum": "bb0892d4a028ae9d282849d05adf851fe54173013f6331e74a4bdd07e4af8dab"
}
]
}
Particularly, I need to replace the partition_size_nominal
tag of the root
partition
The best way I could come up with is very hacky:
#!/bin/bash
fname=partitions.json
tag=partition_size_nominal
newvalue=2942
rootline=$(sed -n '/label.*root/=' $fname | head -1)
blockstart=$(sed -n '1,'"$rootline"'p' $fname | tac | sed -n '/{/=' | head -1)
blockstart=$(( rootline - blockstart + 1 ))
blockend=$(sed -n ''"$blockstart"',$p' $fname | sed -n '/}/=' | head -1)
blockend=$(( blockstart + blockend -1 ))
sed ''"$blockstart"','"$blockend"'s/"'"$tag"'".*/"'"$tag"'": '"$newvalue"',/' $fname
The basic idea is: find root label tag, search backward for beginning of block, search forward for end of block, replace the partition size tag value within block. I can't assume that the partition size tag is after the label tag, hence searching for the block bounds.
Does anyone know of a more elegant solution for this type of situation?