bash 需要使用 sed 更改 json 文件的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45192835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:18:16  来源:igfitidea点击:

need to change values of json file using sed

bashsed

提问by Shachar Hamuzim Rajuan

I need to change values of JSON file using sed,

我需要使用 sed 更改 JSON 文件的值,

I saw a lot of people suggesting using jq, python, or Perl.

我看到很多人建议使用 jq、python 或 Perl。

But I'm working inside a container and I want it to be as simple as possible so only sed is the needed solution for me.

但是我在一个容器内工作,我希望它尽可能简单,所以只有 sed 是我需要的解决方案。

the JSON file is:

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"
  }
}

And I want to change 4 values inside it:

我想更改其中的 4 个值:

"spark.driver.memory" : "1gb",

"spark.driver.memory" : "1gb",

"spark.executor.memory" : "1gb",

"spark.executor.memory": "1gb",

"spark.cores.max" :"1"

“spark.cores.max”:“1”

"period" : "15",

“期间”:“15”,

So the output will be:

所以输出将是:

  {
      "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"
      }
    }

回答by skr

For sed use the following

对于 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

回答by RomanPerekhrest

For that time when you'll be ready for a proper solution using jqtool:

到那时,您将准备好使用jq工具获得正确的解决方案:

jq '.conf |= . + {"spark.driver.memory":"1gb","spark.executor.memory":"1gb","spark.cores.max":"1"} | .schedule |= . + {period:"15"}' file

The output:

输出:

{
  "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"
  }
}

回答by RavinderSingh13

try following awk and let me know if this helps you.

尝试关注 awk,如果这对您有帮助,请告诉我。

awk '{
        match(
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
,/^[[:space:]]+/); val=substr(##代码##,RSTART,RLENGTH) } /spark.driver.memory/ || /spark.executor.memory/{ sub(/[0-9]+/,"1",); } /spark.cores.max/{ sub(/[0-9]+/,"1",) } /period/{ sub(/[0-9]+/,"15",) } { printf("%s%s\n",!/^ +/?val:"",##代码##) } ' Input_file

If you want to save the output of this into same Input_file, then you could save above code's output into a temp file and then save it to Input_file again.

如果要将其输出保存到同一个 Input_file 中,则可以将上述代码的输出保存到临时文件中,然后再次将其保存到 Input_file 中。

EDIT1:a sed solution too now.

EDIT1:现在也是一个 sed 解决方案。

##代码##

If happy with above code's result then use sed -i option to save into the Input_file then.

如果对上述代码的结果感到满意,则使用 sed -i 选项保存到 Input_file 中。