我需要使用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"
}
}
答案 0 :(得分:2)
对于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
答案 1 :(得分:1)
尝试关注awk并告诉我这是否对您有所帮助。
awk '{
match($0,/^[[:space:]]+/);
val=substr($0,RSTART,RLENGTH)
}
/spark.driver.memory/ || /spark.executor.memory/{
sub(/[0-9]+/,"1",$3);
}
/spark.cores.max/{
sub(/[0-9]+/,"1",$2)
}
/period/{
sub(/[0-9]+/,"15",$3)
}
{
printf("%s%s\n",!/^ +/?val:"",$0)
}
' Input_file
如果要将其输出保存到相同的Input_file中,则可以将上面的代码输出保存到临时文件中,然后再将其保存到Input_file。
EDIT1: 现在也是一个解决方案。
sed 's/"spark.driver.memory" : "[0-9]gb"/"spark.driver.memory" : "1gb"/;s/"spark.executor.memory" : "[0-9]gb"/"spark.executor.memory" : "1gb"/;s/"spark.cores.max": "[0-9]"/"spark.cores.max" :"1"/;s/"period" : "[0-9]*"/"period" : "15"/' Input_file
如果对上面代码的结果满意,那么使用sed -i选项保存到Input_file中。
答案 2 :(得分:1)
那时你准备好使用jq
工具做出正确的解决方案了:
jq '.conf |= . + {"spark.driver.memory":"1gb","spark.executor.memory":"1gb","spark.cores.max":"1"} | .schedule |= . + {period:"15"}' file
输出:
{
"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"
}
}