有什么简单的方法可以将json文件“ deploy.json <”中的版本从“ 1.0.2 ”替换为“ 2.6.5 ”。 / strong>”使用groovy脚本,并且文件内容已在下面提供。
{
"versions": [
{
"version": "1.0.2",
"conf": "replian"
},
{
"version": "1.0.2",
"conf": "hp"
},
{
"version": "1.0.2",
"conf": "shutoff"
},
{
"version": "1.0.2",
"conf": "spark"
}
]
}
我尝试了以下操作,但出现错误;
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
def content = """
{
"versions": [
{
"version": "1.0.2",
"conf": "replian"
},
{
"version": "1.0.2",
"conf": "hp"
},
{
"version": "1.0.2",
"conf": "shutoff"
},
{
"version": "1.0.2",
"conf": "spark"
}
]
}"""
def slurped = new JsonSlurper().parseText(content)
def builder = new JsonBuilder(slurped)
builder.content.versions.find{it.version}.version = "2.6.5"
println(builder.toPrettyString())
问题: 仅替换第一个conf版本 例如 { “ version”:“ 2.6.5”, “ conf”:“ replian” }, { “ version”:“ 1.0.2”, “ conf”:“ hp” }, { “ version”:“ 1.0.2”, “ conf”:“关闭” }, { “ version”:“ 1.0.2”, “ conf”:“火花” }
答案 0 :(得分:1)
使用EMAIL_BACKED
:
jq
或者awk,如果必须的话:
$ jq '.versions[].version="2.6.5"' deploy.json
{
"app": "Beach",
"Process": "steam",
"versions": [
{
"version": "2.6.5",
"conf": "replian"
}, ...
一些输出:
$ awk '
BEGIN {
FPAT="([^:]*)|(\"[^\"]+\")"
OFS=":"
}
$1~"\"version\"" {
sub(/"[^"]*"/,"\"2.6.5\"",$2)
}1' deploy.json
答案 1 :(得分:0)
我想建议$ sudo -i
# swapoff -a
# exit
$ strace -eopenat kubectl version
做到这一点。
已编辑::请参见groovy
// Edited Line
答案 2 :(得分:0)
如果您对sed的解决方案感兴趣:sed 's/"version": "1\.0\.2"/"version": "2.6.5"/ deploy.json