我们要根据以下规则编辑json文件
我们需要生成名称car0X_hyb.com -在mazda 字符串下(而X是ID号)
我们需要在我们的Linux机器上更新json文件
范例- ID=1
然后json将为
{
"toyota": [
"car1.com"
],
"mazda": [
"car01_hyb.com"
],
"ford": [
"car01_eng.com"
]
}
ID=4 ,那么json将为
{
"toyota": [
"car1.com"
],
"mazda": [
"car01_hyb.com",
"car02_hyb.com",
"car03_hyb.com",
"car04_hyb.com"
],
"ford": [
"car01_eng.com"
]
}
ID=6 ,那么json将为
{
"toyota": [
"car1.com"
],
"mazda": [
"car01_hyb.com",
"car02_hyb.com",
"car03_hyb.com",
"car04_hyb.com",
"car05_hyb.com",
"car06_hyb.com"
],
"ford": [
"car01_eng.com"
]
}
ID=10 ,那么json将为
{
"toyota": [
"car1.com"
],
"mazda": [
"car01_hyb.com",
"car02_hyb.com",
"car03_hyb.com",
"car04_hyb.com",
"car05_hyb.com",
"car06_hyb.com",
"car07_hyb.com",
"car08_hyb.com",
"car09_hyb.com",
"car10_hyb.com"
],
"ford": [
"car01_eng.com"
]
}
您可以使用生成后续数字流range,使用它们填充模板字符串,并根据结果组成一个数组,例如:
$ jq -n --argjson ID 5 '.mazda = [ range($ID) | "car\("0\(.+1)"[-2:])_hyb.com" ]'
{
"mazda": [
"car01_hyb.com",
"car02_hyb.com",
"car03_hyb.com",
"car04_hyb.com",
"car05_hyb.com"
]
}
$ jq -n --argjson ID 10 '.mazda = [ range($ID) | "car\("0\(.+1)"[-2:])_hyb.com" ]'
{
"mazda": [
"car01_hyb.com",
"car02_hyb.com",
"car03_hyb.com",
"car04_hyb.com",
"car05_hyb.com",
"car06_hyb.com",
"car07_hyb.com",
"car08_hyb.com",
"car09_hyb.com",
"car10_hyb.com"
]
}
根据in字符的最大长度调整前导0s和。-2ID
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。