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

问题描述 投票:0回答:5

我需要使用 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.内存”:“1GB”,

“spark.executor.内存”:“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"
      }
    }
bash sed
5个回答
13
投票

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

5
投票

当您准备好使用

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

2
投票

以下适用于 Mac 和 Linux。

如果您想读取某个变量名称值,请使用第一个命令 如果您有直接价值,请使用第二个命令。

sed -ie 's/"key1": .*"/"key1": '\"${value}\"'/g' filename
sed -ie 's/"key1": .*"/"key1": '\"yourvalue\"'/g' filename


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 解决方案。

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 中。


0
投票

之前的回答者提出了一个解决方案,如果您想读取某个变量名称值,该解决方案将有所帮助......但是更强大的解决方案是:

sed -i "s/\"key1\" : .*\"/\"key1\": \"${value}\"/g"

此解决方案摆脱了

-e
标志和一些导致其无法工作的额外空格。

© www.soinside.com 2019 - 2024. All rights reserved.