首页 > Web开发 > 详细

shell脚本修改json中某个字段的值

时间:2018-10-25 21:31:22      阅读:485      评论:0      收藏:0      [点我收藏+]

shell脚本修改json中某个字段的值

  • 思路:通过awk来找到旧数据,然后用sed来替换旧数据

源码

config.json

{
  "name": "the_name",
  "id": "132869",
  "content_url": "https://hot.example.com/",
  "enable_feature1": "true",
  "enable_feature2": "false"
}

config/mode1.sh

#!bin/bash
content_url_new="https://hot1.example.com/"
enable_feature1_new="true"
enable_feature2_new="true"

config/mode2.sh

#!bin/bash
content_url_new="https://hot2.example.com/"
enable_feature1_new="false"
enable_feature2_new="false"

main.sh

#!bin/bash

if [ "$1" != "mode1" -a "$1" != "mode2" ];then
    echo "tip:─┬─────── 您输入参数不对,请重试:" 
    echo "     │─────── ‘mode1‘, 使用config/mode1.sh的配置"
    echo "     └─────── ‘mode2‘, 使用config/mode2.sh的配置"
    exit 0
fi

case $1 in
mode1) 
    . config/mode1.sh
;;
mode2) 
    . config/mode2.sh
;;
*)
    echo "Usage: sh main.sh [mode1|mode2]"
    exit;
esac

# 如果要修改的内容在文档中唯一,可以做全局修改
content_url_old=$(awk -F"\"" ‘/content_url/{print $4}‘ example.json)
sed -e "s@$content_url_old@$content_url_new@g" -i example.json


# 如果要修改的内容在文档中不唯一,就需要针对那一行做修改。(例如,这个例子中有两个布尔类型的值)
enable_feature1_line=$(awk -F"\"" ‘/enable_feature1/{print NR}‘ example.json) # 记住行号
enable_feature1_old=$(awk -F"\"" ‘/enable_feature1/{print $4}‘ example.json)  # 获取旧数据
sed -e "$enable_feature1_line s@$enable_feature1_old@$enable_feature1_new@" -i example.json # 替换所在行的老数据

enable_feature2_line=$(awk -F"\"" ‘/enable_feature2/{print NR}‘ example.json) # 记住行号
enable_feature2_old=$(awk -F"\"" ‘/enable_feature2/{print $4}‘ example.json)  # 获取旧数据
sed -e "$enable_feature2_line s@$enable_feature2_old@$enable_feature2_new@" -i example.json # 替换所在行的老数据

运行

sh main.sh mode1
sh main.sh mode2

其他方案

shell脚本修改json中某个字段的值

原文:https://www.cnblogs.com/lyloou/p/9852991.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!