我需要使用 sed 更改 JSON 文件的值,
我看到很多人建议使用 jq、python 或 Perl。
但我在容器内工作,我希望它尽可能简单,所以只有 sed 是我需要的解决方案。
JSON 文件是:
{
"useCaseName" : "rca",
"algorithm" : "log-clustering-train",
"mainClass" : "com.hp.analytics.logclustering.MainTrainer",
"applicationJar" : "log-clustering-train-1.0.0-SNAPSHOT-jar-with-dependencies.jar",
"conf" : {
"spark.driver.memory" : "3gb",
"spark.executor.memory" : "9gb",
"spark.executor.userClassPathFirst" : "true",
"spark.cores.max": "8"
},
"schedule" : {
"period" : "10",
"timeUnit" : "hours",
"timeoutPeriodSeconds" : "10800"
}
}
我想更改其中的 4 个值:
"spark.driver.memory": "1gb",
"spark.executor.memory": "1gb",
“spark.cores.max”:“1”
“期间”:“15”,
所以输出将是:
{
"useCaseName" : "rca",
"algorithm" : "log-clustering-train",
"mainClass" : "com.hp.analytics.logclustering.MainTrainer",
"applicationJar" : "log-clustering-train-1.0.0-SNAPSHOT-jar-with-dependencies.jar",
"conf" : {
"spark.driver.memory" : "1gb",
"spark.executor.memory" : "1gb",
"spark.executor.userClassPathFirst" : "true",
"spark.cores.max": "1"
},
"schedule" : {
"period" : "15",
"timeUnit" : "hours",
"timeoutPeriodSeconds" : "10800"
}
}
最佳答案
对于 sed 使用以下内容
sed -i '/spark.driver.memory/c\ \"spark.driver.memory\" : \"1gb\",' file.txt
sed -i '/spark.executor.memory/c\ \"spark.executor.memory\" : \"1gb\",' file.txt
sed -i '/spark.cores.max/c\ \"spark.cores.max\" : \"1\",' file.txt
sed -i '/period/c\ \"period\" : \"15\",' file.txt
关于bash - 需要使用 sed 更改 json 文件的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45192835/