HTML修改json中的值,【Shell】shell脚本修改json中某个字段的值

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

源码

config.jsonhtml

{

"name": "the_name",

"id": "132869",

"content_url": "https://hot.example.com/",

"enable_feature1": "true",

"enable_feature2": "false"

}

config/mode1.shweb

#!bin/bash

content_url_new="https://hot1.example.com/"

enable_feature1_new="true"

enable_feature2_new="true"

config/mode2.shshell

#!bin/bash

content_url_new="https://hot2.example.com/"

enable_feature1_new="false"

enable_feature2_new="false"

#!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 # 替换所在行的老数据

运行bash

sh main.sh mode1

sh main.sh mode2

其余方案

shell中替换json中指定的值 - 矮子爬楼梯 - 博客园sed -i 's/\("enable_feature1":"\).*/\1'"$enable_feature1_to"'",/g' example.json

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值