linux shell环境下处理yml文件

需要安装工具yq

sudo pip install yq

这里我使用pip3指定源安装

sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple yq

1. 读取文件内容:

我使用的版本是yq 3.2.3,以下命令执行会报错:
 

yq r chainmaker.yml .blockchain
yq r chainmaker.yml blockchain
yq e '.blockchain' chainmaker.yml

yq: error: argument files: can't open

经过分析,正确的语法命令如下:
 

cat chainmaker.yml | yq ".blockchain"
yq ".blockchain" chainmaker.yml

2. 转换成json文件

yq '.' chainmaker.yml >>chainmaker.json
yq eval -o=json chainmaker.yml >chainmaker.json 

要安装4.0以上的版本,使用:
访问yq - yq

 GitHub - mikefarah/yq: yq is a portable command-line YAML, JSON, XML, CSV, TOML and properties processor

 Release v4.35.2 - Minor fixes · mikefarah/yq · GitHub

扫描二维码关注公众号,回复: 17228826 查看本文章

下载最新的版本 

解压可以直接使用,也可以配置成默认:

可以使用4.0版本执行以下命令,修改yml文件的内容:

#  输出到另一个文件
./yq eval '.tx_filter.sharding.snapshot.type = 3' chainmaker.yml > config_new2.yml
# 修改当前文件
yq -i eval '.tx_filter.sharding.snapshot.type = 3' chainmaker.yml
# 在type下增加type的子项配置
yq -i eval '.tx_filter.sharding.snapshot.type = {"length": 1000}' chainmaker.yml
# 增加与type平级的配置
yq -i eval '.tx_filter.sharding.snapshot += {"length": 10000}' chainmaker.yml

# 删除键值对:
yq -i eval 'del(.tx_filter.sharding.snapshot.type)' chainmaker.yml

# 处理字符串:
yq -i eval '.txpool.pool_type="normal"' ./chainmaker.yml

# 增加键值对配置:
yq eval '.consensus.ext_config = [{"key": "TBFT_blocks_per_proposer", "value": "100000000"}]' bc1.yml -i

猜你喜欢

转载自blog.csdn.net/fvafuc/article/details/133747114