gpt4 book ai didi

yaml - Linux shell 从 yml 文件中获取一个字段的值

转载 作者:IT王子 更新时间:2023-10-28 23:53:48 27 4
gpt4 key购买 nike

我有一个类似 database.yml 的文件

development:
adapter: mysql2
encoding: utf8
database: d360
host: localhost
username: root
password: password

test:
adapter: mysql2
encoding: utf8
database: sample
host: localhost
username: root
password: password

现在,我想要测试环境数据库的值(即显示的 YAML 的 sample)。我们如何使用 sed 做到这一点?

最佳答案

有比 sed 更好的工具。您可能会被困在一个最小的系统上,但其他寻求答案的人可能不会。

yq的python版本就像jq :

$ pip3 install yq
$ yq -r .test.database database.yml
sample

shyaml也可以工作,但有一个奇怪的限制,你只能重定向到它:

$ pip3 install shyaml
$ shyaml get-value test.database < database.yml
sample

如果 perl 可用并且您安装了 YAML:

$ cpan YAML
$ perl -MYAML -le 'print YAML::LoadFile(shift)->{test}{database}' database.yml
sample

或者,如果你想要 ruby:

$ ruby -r yaml -e 'puts YAML.load_file(ARGV[0])["test"]["database"]' database.yml
sample

关于yaml - Linux shell 从 yml 文件中获取一个字段的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29969527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com